java: JUnit unit testing

background

General software testing is divided into white box testing and black box testing

  • Black box testing: It is the work of general testers now, as long as the function is normal
  • White box testing: you need to pay attention to the specific execution process of the code, and you need to write code

The JUnit framework is used for white box testing.

Not using a unit testing framework

When there is no unit test framework, we write test cases like this:
src/cn.mycompany/myCalculator/Calculator.java

package cn.mycompany.myCalculator;

/**
 * 计算器类
 */
public class Calculator {
    
    
    /**
     * 加法
     * @param a
     * @param b
     * @return
     */
    public int add (int a , int b){
    
    
        return a + b;
    }

    /**
     * 减法
     * @param a
     * @param b
     * @return
     */
    public int sub (int a , int b){
    
    
        return a - b;
    }
}

src/cn.mycompany/myCalculator/CalculatorTest.java

package cn.mycompany.myCalculator;

public class CalculatorTest {
    
    

    public static void main(String[] args) {
    
    
		// 在同一个package,所以不需要导入Calculator
        //创建对象
        Calculator c = new Calculator();
        //调用
       /* int result = c.add(1, 2);
        System.out.println(result);*/

        int result = c.sub(2, 1);
        System.out.println(result);
    }
}

This has several disadvantages:
1. For example, to test addition and subtraction here, you must write out a comment and then write another
2. It is not very intuitive to use println to see whether the use case is executed.
3. It needs to be executed in the main method, not single function execution

Use the unit testing framework JUnit

src/cn.mycompany/test/CalculatorTest.java

package cn.mycompany.test;

import cn.mycompany.myCalculator.Calculator;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class CalculatorTest {
    
    
    /**
     * 初始化方法:
     *  用于资源申请,所有测试方法在执行之前都会先执行该方法
     */
    @Before
    public void init(){
    
    
        System.out.println("init...");
    }

    /**
     * 释放资源方法:
     *  在所有测试方法执行完后,都会自动执行该方法
     */
    @After
    public void close(){
    
    
        System.out.println("close...");
    }


    /**
     * 测试add方法
     */
    @Test
    public void testAdd(){
    
    
       // System.out.println("我被执行了");
        //1.创建计算器对象
        System.out.println("testAdd...");
        Calculator c  = new Calculator();
        //2.调用add方法
        int result = c.add(1, 2);
        //System.out.println(result);

        //3.断言  我断言这个结果是3
        Assert.assertEquals(3,result);

    }

    @Test
    public void testSub(){
    
    
        //1.创建计算器对象
        Calculator c  = new Calculator();
        int result = c.sub(1, 2);
        System.out.println("testSub....");
        Assert.assertEquals(-1,result);
    }
}

You can directly click the run execution on the left side of testAdd.
insert image description here
You can see that the execution in the lower left corner is green, which means success, and if there is red, it means failure.

Guess you like

Origin blog.csdn.net/weixin_43972437/article/details/132104468