Spring Boot测试:是否为每个测试加载了上下文? [英] Spring boot test: context loaded for every test?

查看:120
本文介绍了Spring Boot测试:是否为每个测试加载了上下文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我们所有的测试都有一个超类.这是该类的签名

In my project we have a super class for all our tests. This is the signature of that class

@RunWith(SpringRunner.class)
@SpringBootTest(value = {"management.port=0"}, classes = Application.class, webEnvironment = WebEnvironment.RANDOM_PORT)
@ActiveProfiles({"localhost", "test"})
@ContextConfiguration(classes = {Application.class, SomeConfiguration.class})
@Ignore
public abstract class AIntegrationTest {

其中Application.class是我们的主类,而SomeConfiguration.class仅将它用于某些@Bean和其他内容,没什么花哨的.

Where Application.class is our main class, and SomeConfiguration.class it just for some @Bean and other stuff, nothing fancy.

我使用gradle,并且为了运行测试,我这样做:

I use gradle, and for running my tests I do:

./gradlew :my-project:test

我的问题是:

  • 我不确定是否针对每个测试都初始化了上下文.但是我可以确保上下文被多次初始化.我通过查看日志知道这一点.
  • 由于初始化了多个上下文,因此上下文似乎相互重叠.我知道这是因为症状之一是此异常:

  • I'm not sure if for each test the context is being initialized. But I can assure the context gets initialized multiple times. I know this by looking at the logs.
  • Since multiple contexts are initialized, it seems that contexts overlap with each other. I know this because one of the symptoms is this exception:

Caused by: org.springframework.jmx.export.UnableToRegisterMBeanException: Unable to register MBean [org.springframework.cloud.context.environment.EnvironmentManager@36408d9e] with key 'environmentManager'; nested exception is javax.management.InstanceAlreadyExistsException: RedeemAway:name=environmentManager,type=EnvironmentManager

  • 即使我不关心正在加载的多个上下文,我的印象是,当一个测试完成时,下一个测试会在上一个测试终止之前获得一个新的上下文.我之所以这样说,是因为上面的异常重叠了.

  • Even if I don't care about the multiple contexts being loaded, is my impression that when a test finishes, the next test gets a new context BEFORE the previous one is terminated. I said this because of the overlapping of the exception from above.

    由于所有测试共享同一个JVM,因此当某些bean被两次注册时,该异常上升.通过此链接:

    Since all the tests share the same JVM, when some beans get registered twice, that exception rises up. From this link:

    上下文缓存

    据说:

    ApplicationContext可以通过结合使用来唯一标识用于加载它的配置参数.因此,配置参数的唯一组合用于生成缓存上下文的键.TestContext框架使用以下配置参数来构建上下文缓存键

    An ApplicationContext can be uniquely identified by the combination of configuration parameters that is used to load it. Consequently, the unique combination of configuration parameters is used to generate a key under which the context is cached. The TestContext framework uses the following configuration parameters to build the context cache key

    我了解这一点,但是,我想知道如何实现?我的目标是在同一个JVM上运行所有测试,并在每个测试中重用上下文.

    I understand that, but, I'm wondering how can I achieve that? My goal is to run all my tests over the same JVM and reuse the context with every test.

    2月22日星期四编辑

    EDIT on Thu Feb 22

    我尝试过的事情:

    • spring.jmx.enabled:假
    • spring.jmx.default-domain:某些值

    真正的禁用JMX应该无济于事,因为该例外是针对SpringCloud的EnvironmentManager.

    Really disabling JMX shouldn't help since the excpetion is around the EnvironmentManager, which is from Spring Cloud.

    推荐答案

    我找到了问题的答案.这里有很好的解释:

    I found the answer to my problem. Here is well explained:

    https://github.com/spring-projects/spring-boot/issues/7174

    基本上,如果您运行一堆测试,一旦其中一个开始,如果它使用注释 @MockBean ,它将强制Spring重新加载上下文.

    Basically, if you run a bunch of tests, as soon as one of them gets started, if it uses the annotation @MockBean it will force Spring to reload the context.

    奖金:如果您的测试使用 org.mockito.Mock ,您将看到相同的行为.

    Bonus: you will see the same behavior if your test uses org.mockito.Mock.

    这篇关于Spring Boot测试:是否为每个测试加载了上下文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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