Java教程

通过IntelliJ IDEA进行JUnit4单元测试

本文主要是介绍通过IntelliJ IDEA进行JUnit4单元测试,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1.下载插件

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

下载插件

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

步骤1

步骤2

  • 自动生成的测试类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单元测试的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!