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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40236904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 05:01:34  来源:igfitidea点击:

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

javaspringspring-bootspring-test

提问by Eric Wang

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

我创建了多个 spring-boot 测试类,(使用spring-boot1.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

测试

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

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

The question is:

问题是:

  • Is it possible to share a single spring test context among multiple testing class, and if yes, how?
  • 是否可以在多个测试类之间共享单个 spring 测试上下文,如果是,如何共享?

回答by Phil Webb

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.

通过将两个不同的类与@WebMvcTest(ie@WebMvcTest(FirstAction.class)@WebMvcTest(SecondAction.class)) 一起使用,您明确表示您需要不同的应用程序上下文。在这种情况下,您不能共享单个上下文,因为每个上下文都包含一组不同的 bean。如果你的控制器 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:

如果你真的想要一个可以在所有 web 测试中缓存和共享的上下文,那么你需要确保它包含完全相同的 bean 定义。想到的两个选项:

1) Use @WebMvcTestwithout any controller specified.

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

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 @WebMvcTestat all so you get an application context that contains all beans (not just web concerns)

2) 根本不要使用@WebMvcTest,所以你会得到一个包含所有 bean 的应用程序上下文(不仅仅是 Web 问题)

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.

请记住,缓存上下文可以使多个测试的运行速度更快,但是如果您在开发时重复运行单个测试,您将付出创建大量 bean 的成本,这些 bean 会立即被丢弃。