Add junit unit test to idea

One, download the jar package

Link: https://pan.baidu.com/s/1FzBoZbyGXo6sgG726CBazQ
Extraction code: rmgh
Insert picture description here

Two, add the jar package to the project

Find the jar package location and import

Three, write test cases, test classes

public class Calculator {
    
    

    /**
     * 加法
     *
     * @param a
     * @param b
     * @return a + b
     */
    public int add(int a, int b) {
    
    
        return a + b;
    }

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

Test class

import com.sky.junit.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("我被执行了");
        Calculator calculator = new Calculator();
        int result = calculator.add(1, 2);
        System.out.println(result);
        //用断言操作来处理结构(逾期值,结果值)
        Assert.assertEquals(3, result);
    }
}

Insert picture description here

Fourth, download the plug-in to automatically generate test cases

Insert picture description here

After downloading Apply, ok

1. Setting:

Insert picture description hereOutput path:
S O U R C E P A T H / . . / t e s t / {SOURCEPATH}/../test/ SOURCEPATH/../test/{PACKAGE}/${FILENAME}

2. Create a new test folder equal to src under the module
Insert picture description here3. Modify the coding problem
Insert picture description here4. Modify the JUnit configuration file
Insert picture description here
5. Automatically generate test classesInsert picture description here

Guess you like

Origin blog.csdn.net/zeduo2525/article/details/109297126