I created multiple spring-boot testing class, (with spring-boot
1.4.0).
FirstActionTest.java:
@Ru
By using two different classes with @WebMvcTest
(i.e @WebMvcTest(FirstAction.class)
and @WebMvcTest(SecondAction.class)
) you are specifically indicating that you want different application contexts. You can't share a single context in this case because each context contains a different set of beans. If you're controller beans are fairly well behaved then the context should be relatively quick to create and you shouldn't really have a problem.
If you really want to have a context that can be cached and shared across all web tests, then you need to ensure that it contains exactly the same bean definitions. Two options that spring to mind:
1) Use @WebMvcTest
without any controller specified.
FirstActionTest:
@RunWith(SpringRunner.class)
@WebMvcTest
@TestPropertySource("classpath:test-application.properties")
public class FirstActionTest {
@Autowired
private MockMvc mvc;
// ...
}
SecondActionTest:
@RunWith(SpringRunner.class)
@WebMvcTest
@TestPropertySource("classpath:test-application.properties")
public class SecondActionTest {
@Autowired
private MockMvc mvc;
// ...
}
2) Don't use @WebMvcTest
at all so you get an application context that contains all beans (not just web concerns)
FirstActionTest:
@RunWith(SpringRunner.class)
@SpringBootTest
@TestPropertySource("classpath:test-application.properties")
public class FirstActionTest {
@Autowired
private MockMvc mvc; // use MockMvcBuilders.webAppContextSetup to create mvc
// ...
}
SecondActionTest:
@RunWith(SpringRunner.class)
@SpringBootTest
@TestPropertySource("classpath:test-application.properties")
public class SecondActionTest {
@Autowired
private MockMvc mvc; // use MockMvcBuilders.webAppContextSetup to create mvc
// ...
}
Keep in mind that a cached context can make running multiple tests faster, but if you're repeatedly running a single test at development time, you're paying the cost of creating a lot of beans that then immediately get thrown away.