本文介绍了spring-boot testing - 多个测试可以共享一个上下文吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了多个spring-boot测试类,(带有 spring-boot 1.4.0

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;

    // ...
}

运行测试时通过:

It似乎为每个测试类创建了一个弹簧测试上下文,我猜这是不必要的。

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

问题是:


  • 是否可以在多个测试类之间共享一个弹簧测试上下文,如果是,如何?

推荐答案

使用两个不同的类 @WebMvcTest (即 @ WebMvcTest(FirstAction.class) @WebMvcTest(SecondAction.class))您明确指出您需要不同的应用程序上下文。在这种情况下,您不能共享单个上下文,因为每个上下文包含一组不同的bean。如果你的控制器bean表现得相当好,那么上下文应该相对快速地创建,你应该没有问题。

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.

如果你真的想拥有可以在所有Web测试中缓存和共享的上下文,然后您需要确保它包含完全相同的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. Two options that spring to mind:

1)在没有指定任何控制器的情况下使用 @WebMvcTest

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根本不使用 @WebMvcTest ,这样你就得到一个包含所有bean的应用程序上下文(不仅仅是web问题)

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

    // ...
}

请记住,缓存的上下文可以更快地运行多个测试,但如果你在开发时反复运行单个测试,那么你需要支付创建大量bean的成本,然后立即获得扔掉了。

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.

这篇关于spring-boot testing - 多个测试可以共享一个上下文吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 01:53