JUnit5使用教程及简单的测试案例(Idea,Android studio)

.介绍

什么是Junit5 ?

先看来个公式:

JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage

这看上去比Junit4 复杂,实际上在导入包时也会复杂一些。

JUnit Platform是在JVM上启动测试框架的基础。

JUnit JupiterJUnit5扩展的新的编程模型和扩展模型,用来编写测试用例。Jupiter子项目为在平台上运行Jupiter的测试提供了一个TestEngine (测试引擎)。

JUnit Vintage提供了一个在平台上运行JUnit 3JUnit 4TestEngine 

二.引入

对于JUnit有多种引入,这里介绍Maven和Gradle两种方式

1.Maven(pom.xml)

<dependencies>

    <dependency>
      <groupId>org.junit.platform</groupId>
      <artifactId>junit-platform-launcher</artifactId>
      <version>1.0.1</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-engine</artifactId>
      <version>5.0.1</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.junit.vintage</groupId>
      <artifactId>junit-vintage-engine</artifactId>
      <version>4.12.1</version>
      <scope>test</scope>
    </dependency>

</dependencies>

2.Gradle(app.gradle)

dependencies {
    testCompile('org.junit.jupiter:junit-jupiter-api:5.2.0')
    testRuntime('org.junit.jupiter:junit-jupiter-engine:5.2.0')
}

三.基本方法及注解



四.使用实例

下面我用一个检查手机号码的工具类PhoneNumberCheckUtils来进行简单的测试.

package dream.anywhere.test;

/**
 * Created by ljb on 2018-05-20.
 */


import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

public class PhoneNumberCheckUtils {

    /**
     * 大陆号码或香港号码均可
     */
    public static boolean isPhoneLegal(String str)throws PatternSyntaxException {
        return isChinaPhoneLegal(str) || isHKPhoneLegal(str);
    }

    /**
     * 大陆手机号码11位数,匹配格式:前三位固定格式+后8位任意数
     * 此方法中前三位格式有:
     * 13+任意数
     * 15+除4的任意数
     * 18+除1和4的任意数
     * 17+除9的任意数
     * 147
     */
    public static boolean isChinaPhoneLegal(String str) throws PatternSyntaxException {
        String regExp = "^((13[0-9])|(15[^4])|(18[0,2,3,5-9])|(17[0-8])|(147))\\d{8}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(str);
        return m.matches();
    }

    /**
     * 香港手机号码8位数,5|6|8|9开头+7位任意数
     */
    public static boolean isHKPhoneLegal(String str)throws PatternSyntaxException {
        String regExp = "^(5|6|8|9)\\d{7}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(str);
        return m.matches();
    }
}
然后在代码框右键然后选择 Go to 然后选择Test,然后create new test


然后OK

现在得到了一个简单的测试类,然后补充一下,写几个测试方法

package dream.anywhere.test;

import org.junit.Test;

import static org.junit.Assert.*;

/**
 * Created by 94829 on 2018-05-20.
 */
public class PhoneNumberTest {

    String phoneNumber1 = "15229235980";
    String phoneNumber2 = "15256555459801111";
    String phoneNumber3 = "83749658";



    @Test
    public void isPhoneLegal() throws Exception {
        assertTrue(PhoneNumberCheckUtils.isPhoneLegal(phoneNumber1));
    }

    @Test
    public void isChinaPhoneLegal() throws Exception {
        assertTrue(PhoneNumberCheckUtils.isChinaPhoneLegal(phoneNumber2));
    }

    @Test
    public void isHKPhoneLegal() throws Exception {
        assertTrue(PhoneNumberCheckUtils.isHKPhoneLegal(phoneNumber3));
    }

}

右键Run这个测试类


可以看到,三个方法中有两个方法通过,一个方法未通过,右边给出了未通过方法的出错行

最后附上JUnit5官方文档以及官方案例的地址

文档:   JUnit 5 doc

案例:   JUnit5-samples

猜你喜欢

转载自blog.csdn.net/weixin_40099554/article/details/80387582