Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring-boot testing - Could multiple test share a single context?

Tags:

I created multiple spring-boot testing class, (with spring-boot 1.4.0).

FirstActionTest.java:

@RunWith(SpringRunner.class)
@WebMvcTest(FirstAction.class)
@TestPropertySource("classpath:test-application.properties")
public class FirstActionTest {
    @Autowired
    private MockMvc mvc;

    // ...
}

SecondActionTest.java:

@RunWith(SpringRunner.class)
@WebMvcTest(SecondAction.class)
@TestPropertySource("classpath:test-application.properties")
public class SecondActionTest {
    @Autowired
    private MockMvc mvc;

    // ...
}

When run test via:

mvn test

It seems created a spring test context for each testing class, which is not necessary I guess.

The question is:

  • Is it possible to share a single spring test context among multiple testing class, and if yes, how?
like image 639
user218867 Avatar asked Oct 25 '16 09:10

user218867


1 Answers

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.

like image 62
Phil Webb Avatar answered Sep 18 '22 22:09

Phil Webb