JUnit4 入门

JUnit is a simple framework to write repeatable tests. It is an instance of the xUnit architecture for unit testing frameworks.

一.常用的注解

  • @BeforeClass:在 class 进入之前执行,不管运行多少方法都只会执行一次,static void修饰;
  • @AfterClass:在 class 退出之后执行,不管运行多少方法都只会执行一次,static void修饰;
  • @Before:在每个方法执行之前执行,会执行多次;
  • @After:在每个方法执行之后执行,会执行多次;
  • @Ignore:所修饰的测试方法会被测试运行器忽略
  • @RunWith:可以更改测试运行器 org.junit.runner.Runner
  • @Test:将一个普通的方法修饰成为一个测试方法;
    @Test(expected=XX.class)
    @Test(timeout=毫秒 )

二.入门例子

1.待测试代码:

/**
 * <p>
 * 创建时间为 下午7:16-2018/11/14
 * 项目名称 JavaTools
 * </p>
 *
 * @author shao
 * @version 0.0.1
 * @since 0.0.1
 */
public class Calculate {
    public int add(int a, int b) {
        return a + b;
    }
    public int subtract(int a, int b) {
        return a - b;
    }
    public int multiply(int a, int b) {
        return a * b;
    }
    public int divide(int a, int b) {
        return a / b;
    }
}

测试类

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
/**
 * <p>
 * 创建时间为 下午7:03-2018/11/14
 * 项目名称 JavaTools
 * </p>
 *
 * @author shao
 * @version 0.0.1
 * @since 0.0.1
 */
public class CalculateTest {
    // 在 class 进入之前执行
    @BeforeClass
    public static void testBeforeClass() {
        System.out.println("BeforeClass");
    }
    // 在 class 退出之后执行
    @AfterClass
    public static void testAfterClass() {
        System.out.println("AfterClass");
    }
    // 在每个方法执行之前执行
    @Before
    public void setUp() throws Exception {
        System.out.println("Before");
    }
    // 在每个方法执行之后执行
    @After
    public void tearDown() throws Exception {
        System.out.println("After");
    }
    @Test
    public void add() throws Exception {
        System.out.println(new Calculate().add(1, 2));
    }
    @Test
    public void multiply() throws Exception {
    }
}

执行结果:

BeforeClass
Before
3
After
AfterClass

2.其他测试用法


import org.junit.Ignore;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
 * <p>
 * 创建时间为 下午7:18-2018/11/14
 * 项目名称 JavaTools
 * </p>
 *
 * @author shao
 * @version 0.0.1
 * @since 0.0.1
 */
public class TestException {
    @Test(expected = ArithmeticException.class)
    public void testDivide(){
        assertEquals(6,new Calculate().divide(6,0));
    }
    
    @Ignore("...")
    @Test(timeout=2000)
    public void testWhile() {
        while(true) {
            System.out.println("run forever...");
        }
    }

    @Test(timeout=3000)
    public void testReadFile(){
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

三.测试套件

测试套件就是组织测试类一起运行的
写一个作为测试套件的入口类,这个类不包含其他方法
更改测试类运行器 Suite.class
将要测试类的数组传入到 Suite.SuitesClasses({})

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
/**
 * @author shao
 * @version 0.0.1
 * @since 0.0.1
 */
@RunWith(Suite.class)
@Suite.SuiteClasses({TestTask01.class, TestTask02.class, TestTask03.class})
public class SuitTest {

    /**
     * 1.测试套件就是组织测试类一起运行的
     *
     * 写一个作为测试套件的入口类,这个类不包含其他方法
     * 更改测试类运行器 Suite.class
     * 将要测试类的数组传入到 Suite.SuitesClasses({})
     *
     * */
}

四.多个参数

  • 1.更改默认的测试运行器为RunWith(Parameterized.class)
  • 2.声明变量来存放预期值 和结果值
  • 3.声明一个返回值 为Collection的公共静态方法,并使用@Parameters进行修饰
  • 4.为测试类声明一个带有参数的公共构造函数,并在其中为之声明变量赋值
@RunWith(Parameterized.class)
public class TestParameter {
    int expected = 0;
    int a = 0;
    int b = 0;

    @Parameterized.Parameters
    public static Collection<Object[]> t() {
        return Arrays.asList(new Object[][]{
            {7, 5, 2},
            {8, 6, 2},
            {4, 2, 4},
            {6, 2, 4}
        });
    }

    public TestParameter(int expected, int a, int b) {
        this.expected = expected;
        this.a = a;
        this.b = b;
    }

    @Test
    public void testAdd() {
        assertEquals(expected, new Calculate().add(a, b));
    }
}

五.常用断言

  • assertNull(java.lang.Object object) 检测对象是否为空
  • assertNotNull(Object object) 检测对象是否不为空
  • assertEquals(long expected, long object) 检测对象是否等值
  • asserFalse(boolean flag) 检测是否为假
  • assertTrue(boolean flag)
  • assertSame(Object expected, Object actual) 检测两个对象的是否为同一对象,即引用同一对象
  • assertNotSame(Object expected, Object actual) 检测两个对象的是否不为同一对象,即引用不同一对象
  • fail(String string) 在没有报告的情况下使测试不通过
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 对象的创建与销毁 Item 1: 使用static工厂方法,而不是构造函数创建对象:仅仅是创建对象的方法,并非Fa...
    孙小磊阅读 2,019评论 0 3
  • 概要 64学时 3.5学分 章节安排 电子商务网站概况 HTML5+CSS3 JavaScript Node 电子...
    阿啊阿吖丁阅读 9,285评论 0 3
  • 德邦是怎么发展起来的?阿里巴巴的发展开始于淘宝店铺、靠免佣金而打败当时的美国易趣、以支付宝为支点打开中国电...
    马兴坚阅读 94评论 0 0
  • 2017年8月17日 星期四 今天晚上看《鲁滨逊漂流记》第六章 生活历练 鲁滨逊逐渐适应并改善着自己在孤...
    鑫隆妈妈阅读 121评论 0 0
  • 昨天晚上我和一家人观看了中央一套的电视节目,节目名是“2019寻找最美孝心少年_颁奖典礼”。通过这个节目我认识...
    212dbfa859ff阅读 117评论 0 0