如何将@ClassRule 转换为扩展?

问题描述

我目前正在将我的 WebFlux 应用程序转换为使用 v4 中的 junit 5,我的 gradle.properties 已

let videoNode = SKVideoNode(fileNamed: "video.mov")
    
let skScene = SKScene(size: CGSize(width: planeGeo.width * 1280,height: planeGeo.height * 720))
    
videoNode.position = CGPoint(x: skScene.size.width / 2,y: skScene.size.height / 2)
    
videoNode.size = skScene.size

skScene.addChild(videoNode)

以及独立的wiremock依赖

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

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

有没有人遇到过这些问题并解决了这个问题?我知道有 testImplementation "org.springframework.cloud:spring-cloud-contract-wiremock:${springCloudVersion}" 我可以扩展,但我找不到用于此的工件,谢谢!

解决方法

JUnit Jupiter 对 JUnit 4 Rules 的支持有限,但不包括对 @ClassRule 的支持。

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