我想将junit4迁移到junit5,并且我对junit4使用了@Rule注解。如下所示:
public class A {
@ClassRule
public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();
@Rule
public final SpringMethodRule springMethodRule = new SpringMethodRule();
}我想迁移junit5,但我不知道如何更改规则和ClassRule
这是我的pom.xml部分junit5
<!--junit5-->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>${junit.vintage.version}</version>
<scope>test</scope>
</dependency>
<!-- Only required to run tests in an IDE that bundles an older version -->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-migrationsupport</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>2.17.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>如何在junit5中使用规则和ClassRule?
发布于 2018-07-12 21:05:56
JUnit Jupiter引入了一种新的扩展机制。您必须使用新的available in Spring 5 SpringExtension,而不是规则。也有原作者的backport for Spring 4。
发布于 2021-10-22 17:20:28
因为看起来你还没有使用Spring,所以你不需要“使用新的SpringExtension",你可以只使用JUnit 5中的BeforeAllCallback和AfterAllCallback扩展API。这里有一个示例at Baeldung,大约占页面的2/3。请注意,"TestExtensionContext“现在是"ExtensionContext”。
https://stackoverflow.com/questions/51305382
复制相似问题