如何使用junit5定义@Rule和@ClassRule

时间:2018-07-12 12:11:00

标签: junit5 mockmvc

我想将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,但我不知道如何更改Rule和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上使用Rule和ClassRule?

1 个答案:

答案 0 :(得分:3)

JUnit Jupiter引入了新的扩展机制。您必须使用新的available in Spring 5代替规则SpringExtension。原始作者也有backport for Spring 4