首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用junit5定义@规则和@类规则

如何用junit5定义@规则和@类规则
EN

Stack Overflow用户
提问于 2018-07-12 20:11:00
回答 2查看 4.3K关注 0票数 0

我想将junit4迁移到junit5,并且我对junit4使用了@Rule注解。如下所示:

代码语言:javascript
复制
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

代码语言:javascript
复制
 <!--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?

EN

回答 2

Stack Overflow用户

发布于 2018-07-12 21:05:56

JUnit Jupiter引入了一种新的扩展机制。您必须使用新的available in Spring 5 SpringExtension,而不是规则。也有原作者的backport for Spring 4

票数 4
EN

Stack Overflow用户

发布于 2021-10-22 17:20:28

因为看起来你还没有使用Spring,所以你不需要“使用新的SpringExtension",你可以只使用JUnit 5中的BeforeAllCallback和AfterAllCallback扩展API。这里有一个示例at Baeldung,大约占页面的2/3。请注意,"TestExtensionContext“现在是"ExtensionContext”。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51305382

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档