I have a spring-boot application with several #RestControllers. Now I want to write an #IntegrationTest to test my application.
I have:
#RunWith(SpringJUnit4ClassRunner.class)
#SpringApplicationConfiguration(classes = Application.class)
#WebIntegrationTest("server.port:0")
public class ApplicationTests {
#Test public void someTest() {}
}
this code works fine.
I have splitted my tests in several test classes. One test class per #RestController e.g. TestClass1 and TestClass2.
My question is how do I include those test classes? I am looking for something like the JUnit Suite tests:
#RunWith(Suite.class) and #SuiteClasses({TestClass1.class, ...})
I cannot add another #RunWith to my ApplicationTests class and if I add all these annotations
#RunWith(SpringJUnit4ClassRunner.class)
#SpringApplicationConfiguration(classes = Application.class)
#WebIntegrationTest("server.port:0")
to each of my test classes it causes to start the Tomcat once per test class and this is what I want to avoid. So basically start the Tomcat once and run all tests which are splitted into several test classes.
Thanks
Related
Hi I'm trying to test my Service and Dao layers for a Java EE 7 application.
So I looking for testing solutions follow tutorials using Arquillian with junit test and wildfly remote dependence.
Dao and Service interfaces with relative implementations have been created, following my junit test with Arquillian:
#RunWith(Arquillian.class)
public class GenericServiceTest {
#Inject
private EmployeeService employeeService;
#Deployment
public static JavaArchive createDeployment() {
return ShrinkWrap
.create(JavaArchive.class)
.addAsManifestResource("META-INF/persistence.xml",
"persistence.xml")
.addClasses(EmployeeDao.class, EmployeeDaoImpl.class,
EmployeeService.class, EmployeeServiceImpl.class,
Employee.class)
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
}
#Test
#Transactional
public void should_crud() {
// Gets all the objects
assertNotNull(employeeService);
Employee employee = employeeService.get(new Integer(1));
assertNotNull(employee);
}
}
Running class as JUnit Test it doesn't work with this error:
Caused by: java.lang.Exception: "WFLYCTL0216: Management resource '[(\"deployment\" => \"test.war\")]' not found"
Test pass if any classes has been added to ShrinkWrap as following:
#RunWith(Arquillian.class)
public class GenericDaoTest {
#Inject
private EmployeeService employeeService;
#Deployment
public static JavaArchive createTestableDeployment() {
final JavaArchive jar = ShrinkWrap.create(JavaArchive.class)
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
return jar;
}
#Test
public void should_crud() {
}
}
How can I create a working test using arquillian for Java EE 7 adding service class implementations?
And I have To add every Class and Intefaces that have to be called (for example all entities,dao etc classes) or only Service Interface and implementation Class?
Thanks a lot
Since you're developing a javaee application, I would suggest you to create a War archive instead of Jar.
You can add the whole package using
ShrinkWrap.addPackages(true, "com.yourpackage.name") so you don't have to add your classes independently.
If I understand the question correctly you want to test a war archive.
If this is the case you should change
return ShrinkWrap
.create(JavaArchive.class)
to
return ShrinkWrap
.create(WarArchive.class)
In addition you should add your persistence.xml file to the META-INF folder like:
.addAsResource("test-persistence.xml", "META-INF/persistence.xml")
If you want to use the annotation #Transactional in your tests, you need to add a few dependencies to your test scope. If you didn't add them yet you can read at http://arquillian.org/modules/transaction-extension/ what dependencies to add.
Now, I am trying to develop a testing tool, which can make unit testing. I mean I want to use JUnit in my testing tool to test other projects. But I don't know how to insert JUnit into my testing tool. Is it possible to do that and how? And is there any other open-source testing tool can be inserted into my testing tool?
To use Junit API make sure you got the jar in the classpath
To use Junit tests you need the testing class to extend SystemTestCase4
and your function to have #Test annotation above it
if you want code to run before your test so use function with #Before
and if you want after use function with #After
public class BaseTest extends SystemTestCase4 {
#Before
public void beforeEachTest() throws Exception {
}
#Test
#TestProperties(name = "test test ")
public void testTest() throws Exception {
//run your tested code
}
#After
public void afterEachTest() throws Exception {
}
as for how to test your projects it depend what tests you want to do?
For unit testing just add your own tests inside the projects
Integration, Functional or other tests need you to understand how to "Attack" it, meaning if it's UI tests for web so use tools for that (Selenium for example) if it's for checking network so use JMeter
I have a question regarding setting up Junit Test Suite. First of all, I am using JUnit libarary version 4.7.
My problem is to create application context in the test suite then allow all the test suite it is running to be accessible to the application context. I need the application context is because some unit test is using it to populate required data. So I am reusing some class to do the deeds.
The following is my snipplet:
#RunWith(Suite.class)
#SuiteClasses({RestServiceUnitTest.class})
public class IntegrationSuite {
public ApplicationContext appCtx;
#BeforeClass
public static void setUp() {
System.out.println("setting up in Suite");
appCtx = new FileSystemXmlApplicationContext("/integrationtest/rest_test_app_ctx.xml" );
}
#AfterClass
public static void tearDown() {
System.out.println("tearing down in Suite");
CleanUp cleaner = (CleanUp)appCtx.getBean("cleanup");
cleaner.clean();
}
}
I need the appCtx to be accessible to the RestServiceUnitTest class. May I know how is this is done ? Anyone has any idea.
I guess it is pretty similiar to :
Test invocation: how to do set up common to all test suites
But I not sure how to access the variable you created in the testsuite class in unittest as the answer is not mentioning it.
What is the best practice to test a Grails Service which depends on another Service?
The default mixin TestFor correctly inject the service under test, for eg:
#TestFor(TopService)
class TopServiceTests {
#Test
void testMethod() {
service.method()
}
}
but if my instance of TopService (service) relies on another Service, like InnerService:
class TopService {
def innerService
}
innerService will not be available, dependency injection doesn't seem to fill this variable. How should I proceed?
Integration tests should not use the #TestFor annotation, they should extend GroovyTestCase. The test annotations are only for unit tests (and will have bad behavior when used in integration tests, especially the #Mock annotations). You're seeing one of those bad behaviors now.
If you extend GroovyTestCase you can then just have
def topService
At the top of your test and it'll get injected with all of it's dependencies injected.
For a unit test case, you'd just want to add new instances of associated services to your service in a setUp method. Just like:
#TestFor(TopService)
class TopServiceTests {
#Before public void setUp() {
service.otherService = new OtherService()
}
...
I have a CustomerRegistrationServiceTest and my CustomerRegistrationService depends on the PasswordService.
my CustomerRegistrationService just autowires it like normal:
class CustomerRegistrationService {
def passwordService
In my CustomerRegistrationServiceTest I have:
#TestFor(CustomerRegistrationService)
#Mock(Customer)
class CustomerRegistrationServiceTests extends GrailsUnitTestMixin {
void setUp() {
mockService(PasswordService)
}
So when I test the CustomerRegistrationService, it is able to access the PasswordService
I want to setup my testing database with test data before I start my tests. I suppose I should run that once at the start of the unit tests instead of before each test class for function? How might I do that?
You can achieve that with #SuiteClasses annotation:
#RunWith(Suite.class)
#SuiteClasses({UserDaoTests.class, OrderDaoTests.class})
public class TestSuiteSetup {
#BeforeClass
public static void setUpDatabase() {
// ...
}
#AfterClass
public static void tearDownDatabase() {
// ...
}
}
Tests from UserDaoTests and OrderDaoTests will be run between setUpDatabase and tearDownDatabase methods.
The accepted solution to How to load DBUnit test data once per case with Spring Test will do this. It works across an arbitrary set of test cases.
For what it's worth, TestNG supports this with #BeforeSuite and #AfterSuite (and many more configuration annotations).