Across application testing
It is possible to test your @AcrossApplication
with Spring Boot testing support.
Using the @SpringBootTest
in your tests is necessary for default application.properties loading and YAML support.
Refer to the Spring Boot reference documentation for more information on Spring Boot testing facilities.
@WebIntegrationTest
A full-stack web integration test (eg. with an embedded Tomcat) does not require any special configuration.
You can use @ExposeForTest
in combination with @SpringBootTest
to expose additional components during test execution.
@RunWith(SpringJUnit4ClassRunner.class)
@WebIntegrationTest(randomPort = true)
@SpringBootTest(classes = MyApplication.class)
public class TestSpringBootWebIntegration
{
private final TestRestTemplate restTemplate = new TestRestTemplate();
@Value("${local.server.port}")
private int port;
@Test
public void controllersShouldSayHello() {
assertEquals( "application says hello", get( "/application" ) );
assertEquals( "infrastructure says hello", get( "/infrastructure" ) );
}
private String get( String relativePath ) {
return restTemplate.getForEntity( url( relativePath ), String.class ).getBody();
}
private String url( String relativePath ) {
return "http://localhost:" + port + relativePath;
}
}
Integration test with MockMvc support
It is also possible to start your entire @AcrossApplication
without an embedded servlet container.
However if you want to have support for MockMvc
instantiated with dynamically registered filters, you must manually configure the application loader to use a MockAcrossServletContext
.
This is done by adding the required configuration to @SpringBootTest
and initializer classes to the @ContextConfiguration
.
See the appendix.adoc#test-configuration-classes for an overview of all configuration classes available.
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@SpringBootTest( classes = { MyApplication.class, MockMvcConfiguration.class } )
@ContextConfiguration( initializers = MockAcrossServletContextInitializer.class )
public class TestSpringBootMockMvc
{
@Autowired
private MockMvc mockMvc;
@Test
public void controllersShouldSayHello() throws Exception {
assertContent( "application says hello", get( "/application" ) );
assertContent( "infrastructure says hello", get( "/infrastructure" ) );
}
private void assertContent( String expected, RequestBuilder requestBuilder ) throws Exception {
mockMvc.perform( requestBuilder )
.andExpect( status().isOk() )
.andExpect( content().string( is( expected ) ) );
}
}
Using @AcrossWebAppConfiguration in combination with @SpringBootTest is not possible, as the context loaders they use conflict with each other.
|