Junit基础

1:基本要求

  1)测试方法上必须使用@Test进行修饰

  2)测试方法必须使用public void 进行修饰,不带任何参数

  3)新建源码目录来存放测试的代码

业务逻辑类

package com.mycompany.junit_maven;
public class Calculate {

    public int add(int i, int j) {
     
        return i + j;
    }

    public int sub(int i, int j) {
        return i - j;
    }

    public int mul(int i, int j) {
        return i * j;
    }

    public int div(int i, int j) {
        System.out.println("--------------div");
        return i / j;
    }


}

测试类

package com.mycompany.junit_maven;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Ignore;

public class CalculateTest {

    Calculate calculate = new Calculate();

    public CalculateTest() {
    }

    @BeforeClass
    public static void setUpClass() {
        System.out.println("--------BeforeClass");
    }

    @AfterClass
    public static void tearDownClass() {
        System.out.println("-------AfterClass");
    }

    @Before
    public void setUp() {
        System.out.println("-------Before");
    }

    @After
    public void tearDown() {
        System.out.println("-------After");
    }

    @Test(timeout = 12)//—超时 毫秒
    public void testAdd() {
        System.out.println("-------Testadd");

        int r = calculate.add(3, 5);
        assertEquals(8, r);
    }

    @Test
    public void testDiv() {
        System.out.println("--------------Testdiv");

        int r = calculate.div(6, 2);
        assertEquals("+++++++++++++++++++++++++++++++++++++++++++", 3, r);
    }



    @Test(expected = Exception.class)
    @Ignore
    public void testDiv2() {
        calculate.div(23, 0);
    }
//   @Ignore("++++++++++++++++++++++++++++++++")
//   @Test
//   public void testDiv3(){
//        c.div(23, 0);
//    }


}

测试流程:

  @BeforeClass:所修饰的方法所有方法被调用前执行,而且该方法是静态的,所以当测试类被加载后,接着会运行它,在内存中,它只存在一份实例。

  @AfterClass:通常用来对资源的清理

  @Before @After 会在每个测试方法的前后各执行一次

2:测试错误和失败

  Failure:一般由单元测试使用的断言方法判断失败引起的,这表示测试点出了问题,程序输出结果和我们的预期不一样

  Error:是由代码异常引起的,产生于测试代码本身的错误,被测试代码中隐藏bug,测试用例不是用来证明你是对的,而是用来证明你没有错
3:注解

  @Runnwith:可以更改测试运行器

  @Ignore:会被测试运行期忽略

4:参数化测试

  更改测试运行期,然后写一个返回值为集合的静态方法,这个方法运行在构造函数之前,用来进行赋值的工作。测试类只能由一个构造函数,而且还是带参数的。

package com.mycompany.junit_maven;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class CalculateParameterTest {

    private int a;
    private int b;
    private int expected;
    private Calculate calculate=new Calculate();

    public CalculateParameterTest(int a, int b, int expected) {
        System.out.println("*********************************public CalculateParameterTest(int a,int b,int expected)");
        this.a = a;
        this.b = b;
        this.expected = expected;
    }

    @Parameters
    public static Collection<Integer[]> getParameters() {
        System.out.println("************************************* public static Collection<Integer[]> getParameters()");
        List<Integer[]> parameters = new ArrayList<Integer[]>();
        Integer[] parameter;
        parameter = new Integer[]{1, 2, 3};
        parameters.add(parameter);
        parameter = new Integer[]{-1, 2, 1};
        parameters.add(parameter);
        parameter = new Integer[]{2, 2, 4};
        parameters.add(parameter);
        parameter = new Integer[]{4, 0, 4};
        parameters.add(parameter);
        return parameters;
    }
//    @Parameters
//    public static Collection<Integer[]> getParameters() {
//        Integer[][] inputArr;
//        inputArr = new Integer[][]{{1, 2, 3}, {-1, 2, 1}, {2, 2, 3}, {4, 0, 4}};
//        return Arrays.asList(inputArr);
//    }

    @Test
    public void testAdd() {
        System.out.println("*********************************************public void testAdd()");
        assertEquals(expected, calculate.add(a, b));
    }

}

5:测试套件

  1)测试套件就是组织测试类一起运行的

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

package com.mycompany.junit_maven;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;


@RunWith(Suite.class)
@Suite.SuiteClasses({com.mycompany.junit_maven.CalculateParameterTest.class, com.mycompany.junit_maven.CalculateTest.class})
public class SuiteTest {

}
原文地址:https://www.cnblogs.com/zhao307/p/5394278.html