简体   繁体   English

spring-boot testing - 多个测试可以共享一个上下文吗?

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

I created multiple spring-boot testing class, (with spring-boot 1.4.0 ) . 我创建了多个spring-boot测试类, (使用spring-boot 1.4.0

FirstActionTest.java : FirstActionTest.java

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

    // ...
}

SecondActionTest.java : 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 mvn测试

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? 是否可以在多个测试类之间共享一个弹簧测试上下文,如果是,如何?

By using two different classes with @WebMvcTest (ie @WebMvcTest(FirstAction.class) and @WebMvcTest(SecondAction.class) ) you are specifically indicating that you want different application contexts. 通过使用两个不同的类与@WebMvcTest (即@WebMvcTest(FirstAction.class)@WebMvcTest(SecondAction.class) ),您明确指出您需要不同的应用程序上下文。 You can't share a single context in this case because each context contains a different set of beans. 在这种情况下,您不能共享单个上下文,因为每个上下文包含一组不同的bean。 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. 如果你的控制器bean表现得相当好,那么上下文应该相对快速地创建,你不应该真的有问题。

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. 如果您确实希望在所有Web测试中都有一个可以缓存和共享的上下文,那么您需要确保它包含完全相同的bean定义。 Two options that spring to mind: 我想到两个选择:

1) Use @WebMvcTest without any controller specified. 1) @WebMvcTest没有指定任何控制器的情况下使用@WebMvcTest

FirstActionTest: FirstActionTest:

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

    // ...
}

SecondActionTest: 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) 2)不要使用@WebMvcTest ,因此你得到一个包含所有bean的应用程序上下文(不仅仅是web问题)

FirstActionTest: FirstActionTest:

@RunWith(SpringRunner.class)
@SpringBootTest
@TestPropertySource("classpath:test-application.properties")
public class FirstActionTest {
    @Autowired
    private MockMvc mvc; // use MockMvcBuilders.webAppContextSetup to create mvc

    // ...
}

SecondActionTest: 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. 请记住,缓存的上下文可以更快地运行多个测试,但如果您在开发时反复运行单个测试,则需要支付创建大量bean的成本,然后立即将其丢弃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM