【问题标题】:How to debug, why Spring Boot integration test context is not reused between tests?如何调试,为什么 Spring Boot 集成测试上下文没有在测试之间重用?
【发布时间】:2020-07-26 04:27:00
【问题描述】:

我有另一个棘手的问题要问人群。我有两个单独的测试文件重用同一个 Context 类。我希望他们重用相同的上下文,唉 Spring ist 启动了两次,从而延长了构建时间。你有什么想法如何弄清楚/调试,是什么触发了上下文重新加载?

测试类如下所示:

@SpringBootTest(
  classes = [HttpProxyTestContext::class]
)
@AutoConfigureWireMock(port = 8082)
internal class AuthOpenidConnectSpringIT {
...
}

@SpringBootTest(
  classes = [HttpProxyTestContext::class]
)
@AutoConfigureWireMock(port = 8082)
internal class AuthOidcWebClientIT {
...
}

上下文类是

@JooqTest
@ComponentScan(basePackages = ["de.denktmit.someproject.springconfig"])
class HttpProxyTestContext {}

最好的问候,保持健康,

马吕斯·施密特

【问题讨论】:

  • 这就是我理解 Spring Test Framework 应该工作的方式:。仅为一个测试类保留上下文。
  • 嗨@johanneslink,根据文档docs.spring.io/spring-framework/docs/current/…,如果我理解正确,默认是重用上下文。但我似乎以某种方式打破了它,我不明白,如何。
  • 嗯。我最近深入研究了 testcontext 代码的某些部分。上下文肯定是每个测试类创建和存储的。当从相同配置创建上下文时,可能会有额外的缓存。我明天去看看。

标签: spring-boot kotlin integration-testing junit5


【解决方案1】:

我终于能够弄清楚,问题是由于使用@AutoConfigureWireMock(port = 8082) 引起的。我通过尝试使用的注释发现了它。最后我稍微调整了我的测试设置,最后我的上下文被重用了。这是我的做法,这是我的测试专用配置:

@SpringBootConfiguration
@ComponentScan(basePackages = ["de.denktmit.someproject.springconfig"])
@AutoConfigureJooq
class HttpProxyTestContext {

  @Bean(destroyMethod = "stop")
  fun wiremock(): WireMockServer {
    val wireMockServer = WireMockServer(wireMockConfig().port(8082))
    wireMockServer.start()
    WireMock.configureFor("localhost", 8082);
    return wireMockServer
  }

}

而我就像被接起来一样容易

@SpringBootTest
internal class AuthOpenidConnectSpringIT {
  ...
}

@SpringBootTest
internal class AuthOidcWebClientIT {
   ...
}

Wiremock @BeforeEach 设置保持不变,与以前相同。愿它有所帮助。

晚安,问候,

马吕斯·施密特

【讨论】:

    猜你喜欢
    • 2023-03-22
    • 2017-10-29
    • 2020-02-25
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 2017-07-30
    • 2015-08-20
    • 2020-04-24
    相关资源
    最近更新 更多