在所有SpringBootTests中使用一个spring引导上下文 [英] Use one spring boot context through all SpringBootTests

查看:249
本文介绍了在所有SpringBootTests中使用一个spring引导上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够使用junit通过不同的类缓存应用程序上下文。

I want to be able to cache application context through different classes with tests using junit.

测试类以这种方式声明:

Test classes are declared this way:

@SpringBootTest
@RunWith(SpringRunner.class)
public class SomeIntegrationTest {
}

我看到了这个问题在junit测试类中重用spring应用程序上下文但在这种情况下我不使用任何xml,我想完全启动上下文,而不仅仅是几个bean,所以 @SpringBootTest @ContextConfiguration 更合适,如果我做对了。

I saw this question Reuse spring application context across junit test classes but in this case I don't use any xml and I want to start context completely, not just few beans from it, so @SpringBootTest is more suitable than @ContextConfiguration, if I got it right.

推荐答案

Ruslan,所以你的问题是关于如何为JUnit Suite重用Spring Boot Context,对吗?

Ruslan, so your question is on how to reuse the Spring Boot Context for a JUnit Suite, right?

然后,它几乎是开箱即用的,您只需要使用 @SpringBoot注释每个单元测试测试注释。

Then, it's almost out-of-the-box provided, you just need to annotate each unit test with the @SpringBootTest annotation.

还要确保主 @SpringBootApplication 类正在加载所有必要的 @Configuration 类,如果 @SpringBootApplication 在根包上面,那么这个过程将自动完成所有配置class和继承的 @ComponentScan 将加载所有这些。

Also make sure that your main @SpringBootApplication class is loading all the necessary @Configuration classes, this process will be automatically done if the @SpringBootApplication is on the root package above all configuration class and with the inherited @ComponentScan will load up all of them.

来自Spring Boot Testing文档:


Spring Boot提供了一个 @SpringBootTest 注释,它可以当您需要Spring Boot功能时,可以用作标准spring-test @ContextConfiguration 注释的替代方法。注释的工作原理是通过SpringApplication创建测试中使用的ApplicationContext。
Spring TestContext框架将应用程序上下文存储在静态缓存中。这意味着上下文实际上存储在静态变量中。换句话说,如果测试在单独的进程中执行,则静态高速缓存将在每次测试执行之间被清除,这将有效地禁用高速缓存机制。
要从缓存机制中受益,所有测试必须在同一进程或测试套件中运行。这可以通过在IDE中作为一个组执行所有测试来实现

Spring Boot provides a @SpringBootTest annotation which can be used as an alternative to the standard spring-test @ContextConfiguration annotation when you need Spring Boot features. The annotation works by creating the ApplicationContext used in your tests via SpringApplication. The Spring TestContext framework stores application contexts in a static cache. This means that the context is literally stored in a static variable. In other words, if tests execute in separate processes the static cache will be cleared between each test execution, and this will effectively disable the caching mechanism. To benefit from the caching mechanism, all tests must run within the same process or test suite. This can be achieved by executing all tests as a group within an IDE

来自Spring Testing文档:


默认情况下,加载后,配置的ApplicationContext将重复用于每个测试。因此,每个测试套件仅产生一次设置成本,并且后续测试执行要快得多。在此上下文中,术语测试套件意味着所有测试都在同一个JVM中运行

By default, once loaded, the configured ApplicationContext is reused for each test. Thus the setup cost is incurred only once per test suite, and subsequent test execution is much faster. In this context, the term test suite means all tests run in the same JVM

检查此网址:

  • http://docs.spring.io/spring/docs/current/spring-framework-reference/html/integration-testing.html#testcontext-ctx-management-caching
  • http://docs.spring.io/spring/docs/current/spring-framework-reference/html/integration-testing.html#testing-ctx-management

  • 使用 @SpringBootTest

  • Annotate each unit test with @SpringBootTest

加载所有bean和必要的配置主要 @SpringBootApplication

Load all beans and necessary configuration classes in your main @SpringBootApplication class

重要类重要提示:运行一个JUnit Suite,而不是单个JUnit测试。在IDE中作为一个组执行所有测试。

这篇关于在所有SpringBootTests中使用一个spring引导上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆