通过IntelliJ IDEA进行JUnit4单元测试

1.下载插件

File->settings->Plguins->选择JUnit Generator V2.0安装。

通过IntelliJ IDEA进行JUnit4单元测试

2.下载jar包

官网传送门

导入jar包的过程就不赘述了

3.单元测试

以加减乘除类为例

  • Calculate.java
package Main;

/*
 *  Calculate.java
 *  2021-10-14 22:23
 *
 */

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) {
        if (b == 0) {
            System.out.print("分母不能为0");
            return 0;
        } else {
            return (a / b);
        }
    };

}

在类中,右键选择Generate-JUnit Test-JUnit4,然后会自动生成测试类CalculateTest.java

通过IntelliJ IDEA进行JUnit4单元测试

通过IntelliJ IDEA进行JUnit4单元测试

  • 自动生成的测试类CalculateTest.java
import org.junit.Test; 
import org.junit.Before; 
import org.junit.After; 

/** 
* Calculate Tester. 
* 
* @author z1zhang@foxmail.com 
* @version 1.0 
*/ 
public class CalculateTest { 

@Before
public void before() throws Exception { 
} 

@After
public void after() throws Exception { 
} 

/** 
* 
* Method: add(int a, int b) 
* 
*/ 
@Test
public void testAdd() throws Exception { 
//TODO: Test goes here... 
} 

/** 
* 
* Method: subtract(int a, int b) 
* 
*/ 
@Test
public void testSubtract() throws Exception { 
//TODO: Test goes here... 
} 

/** 
* 
* Method: multiply(int a, int b) 
* 
*/ 
@Test
public void testMultiply() throws Exception { 
//TODO: Test goes here... 
} 

/** 
* 
* Method: divide(int a, int b) 
* 
*/ 
@Test
public void testDivide() throws Exception { 
//TODO: Test goes here... 
} 


} 
  • 修改后的CalculateTest.java
package test.Main; 

import Main.Calculate;
import org.junit.Test;
import org.junit.Before; 
import org.junit.After;

import static org.junit.Assert.assertEquals;

/** 
* Calculate Tester. 
* 
* @author z1zhang@foxmail.com 
* @version 1.0 
*/ 
public class CalculateTest {

    Calculate calculate = new Calculate();

    @Before
    public void before() throws Exception {
        System.out.println("测试开始");
    }

    @After
    public void after() throws Exception {
        System.out.println("测试结束");
    }

    /**
     * Method: add(int a, int b)
     */
    @Test
    public void testAdd() throws Exception {
        assertEquals(100, calculate.add(99, 1));
        assertEquals(60, calculate.add(9, 51));
    }

    /**
     * Method: subtract(int a, int b)
     */
    @Test
    public void testSubtract() throws Exception {
        assertEquals(98, calculate.subtract(99, 1));
        assertEquals(48, calculate.subtract(50, 2));
    }

    /**
     * Method: multiply(int a, int b)
     */
    @Test
    public void testMultiply() throws Exception {
        assertEquals(90, calculate.multiply(10, 9));
        assertEquals(1000, calculate.multiply(8, 125));
    }

    /**
     * Method: divide(int a, int b)
     */
    @Test
    public void testDivide() throws Exception {
        assertEquals(3, calculate.divide(9, 3));
        assertEquals(0, calculate.divide(5, 0));
    }

}

4.测试结果

通过IntelliJ IDEA进行JUnit4单元测试
我的个人博客

上一篇:@Transactional


下一篇:53-其实这些都是编译期异常,就是写代码不报错但是有可能编译时候出问题所以提前声明了异常这样理解-异常处理的第二种方式-捕获异常try catch throws是抛 catch是捕