Integration Testing
SeedStack provides several features and tools to do integration testing in your application.
The purpose of integration tests is to test the interaction of multiple components together. They are different from unit tests which test only a component (a class) at a time.
JUnit 4
To run an integration test with JUnit 4, you need to add the following dependency in your application:
<dependency>
<groupId>org.seedstack.seed</groupId>
<artifactId>seed-testing-junit4</artifactId>
<scope>test</scope>
</dependency>
Show version
dependencies {
testCompile("org.seedstack.seed:seed-testing-junit4:3.15.1")
}
Then you need to specify that your test class will run using the SeedITRunner
JUnit runner:
@RunWith(SeedITRunner.class)
public class SomeIT {
@Inject
private Application application;
@Test
public void myTestIsInjected() {
Assertions.assertThat(application).isNotNull();
}
}
Test class instances will be injected and can be intercepted (for instance for transaction testing).
Other testing frameworks
SeedStack can support multiple testing frameworks but ships only with JUnit 4 support for now.
Testing features
Regardless of the testing framework, SeedStack provides several features to help altering the application environment during testing.
Altering launch
By default, SeedStack starts the tested application using a built-in launcher that simulates a command-line environment.
This can be changed by applying the @LaunchWith
annotation on the test class:
@RunWith(SeedITRunner.class)
@LaunchWith(MyCustomLauncher.class)
public class SomeIT {
@Test
public void testSomething() {
// ...
}
}
With this annotation, you can specify:
- The implementation of
SeedLauncher
to use to run the tested application. - The
LaunchMode
of the tested application: once per test class, once per test or never (in which case it should be started by other means).
Altering configuration
You can alter the configuration of the tested application by using the @ConfigurationProperty
annotation, on the test class, on a particular test method or both:
@RunWith(SeedITRunner.class)
@ConfigurationProperty(name = "someTestKey", value = "testValue")
public class SomeIT {
@Test
public void testSomething() {
// ...
}
}
You can also activate a configuration profile on the tested application
by using the @ConfigurationProfiles
annotation, on the test class, on a particular
test method or both:
@RunWith(SeedITRunner.class)
@ConfigurationProfiles({"dev", "debug"})
public class SomeIT {
@Test
public void testSomething() {
// ...
}
}
Altering system properties
You can alter the system properties of the tested application by using the @SystemProperty
annotation, on the test class, on a particular test method or both:
@RunWith(SeedITRunner.class)
@SystemProperty(name = "someTestProperty", value = "testValue")
public class SomeIT {
@Test
public void testSomething() {
// ...
}
}
Altering arguments
You can specify the arguments passed to the launcher by using the @Arguments
annotation, on the test class, on a particular test method or both:
@RunWith(SeedITRunner.class)
@Arguments({"-o", "someValue"})
public class SomeIT {
@Test
public void testSomething() {
// ...
}
}
When the @Arguments
annotation is used on a test method, the
launch mode must be set to PER_TEST
, otherwise it is ignored.
Altering kernel parameters
You can alter the SeedStack kernel parameters by using the @KernelParameter
annotation, on the test class, on a particular test method or both:
@RunWith(SeedITRunner.class)
@KernelParameter(name = "seedstack.autodetectModules", value = "false")
public class SomeIT {
@Test
public void testSomething() {
// ...
}
}
When the @KernelParameter
annotation is used on a test method, the
launch mode must be set to PER_TEST
, otherwise it is ignored.
Expecting a launch exception
You can test for a particular exception to occur during the launch of the tested application by using the
@Expected
annotation on the test class, on a particular test method or both:
@RunWith(SeedITRunner.class)
@Expected(SomeException.class)
public class SomeIT {
@Test
public void testSomething() {
// ...
}
}
When the @Expected
annotation is used on a test method, the
launch mode must be set to PER_TEST
, otherwise it is ignored.