JUNIT5(maven配置)

JUNIT5开始测试的写法发生了很大的改变

按照官网的说法现在的junit变成了下面的这个样子

JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage

基本的测试方法都放到了JUnit Jupiter 里边

所以如果代码写成下面这个样子在maven里是跑不了的

package FirstJunit.JTest;

import static org.junit.Assert.*;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.junit.platform.runner.JUnitPlatform;

@RunWith(JUnitPlatform.class)
public class FirstJUnit5Tests {
    @Test
   public  void myFirstTest() {
        assertEquals(2, 1 + 1);
    }
    
    @BeforeAll
    static void initAll() {
        System.out.print("fff");
    }

    @BeforeEach
    public void init() {
        System.out.print("start init");
    }
}

所以maven的plugin要做出一定的修正

下面这个来自于junit5的官方说明,主要是要修改maven-surefire-plugin中的内容将junit-platform-surefire-provider给包含进去

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
    <junit.version>4.12</junit.version>
    <junit.jupiter.version>5.0.2</junit.jupiter.version>
    <junit.vintage.version>${junit.version}.2</junit.vintage.version>
    <junit.platform.version>1.0.2</junit.platform.version>
  </properties>
 <build>
        <plugins>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
                <configuration>
                    <includes>
                        <include>**/Test*.java</include>
                        <include>**/*Test.java</include>
                        <include>**/*Tests.java</include>
                        <include>**/*TestCase.java</include>
                    </includes>
                    <properties>
                        <!-- <includeTags>fast</includeTags> -->
                        <excludeTags>slow</excludeTags>
                        <!--
                        <configurationParameters>
                            junit.jupiter.conditions.deactivate = *
                        </configurationParameters>
                        -->
                    </properties>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.junit.platform</groupId>
                        <artifactId>junit-platform-surefire-provider</artifactId>
                        <version>${junit.platform.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
</build>

这样在运行maven test的时候就可以进行正常的测试。

还有就是如果在类的开头不写@RunWith(JUnitPlatform.class)的话 会造成 junit插件的不识别,导致 run as junit test 选项不出现。

配置方面就是这个样子。

 

转载于:https://www.cnblogs.com/spchenjie/p/8016833.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值