首页 > 解决方案 > 如何将@ClassRule 转换为扩展?

问题描述

我目前正在转换我的 WebFlux 应用程序以使用 v4 的 junit 5,我的 gradle.properties 有

testImplementation ("org.springframework.boot:spring-boot-starter-test") {
exclude group : 'org.junit.vintage', module: 'junit-vintage-engine'
}

以及独立的wiremock依赖

testImplementation "org.springframework.cloud:spring-cloud-contract-wiremock:${springCloudVersion}"

我试图找出一种在junit 5中加载classRule或将其转换为扩展的方法,这似乎是最减少代码的方法。

有没有人遇到过这些问题并解决了这个问题?我知道WireMockExtention.class我可以扩展,但我找不到这个工件,谢谢!

标签: javaspring-webfluxjunit5wiremock

解决方案


JUnit Jupiter 对JUnit 4 规则的支持有限,但不包括对@ClassRule.

因此,如果您有为 JUnit 4 编写的自定义类规则,最好的办法是使用 JUnit Jupiter 中的BeforeAllCallbackAfterAllCallback扩展 API 重写它。然后,您可以通过或注册您的分机@ExtendWith@RegisterExtension


推荐阅读