通过IntelliJ IDEA进行JUnit4单元测试

本文介绍了如何下载并使用JUnitGeneratorV2.0插件自动生成单元测试代码,以Calculate类为例,展示了加减乘除方法的测试用例。在CalculateTest.java中,为每个方法添加了具体的断言测试,如加法和除法运算的预期结果。测试完成后,可以看到详细的测试结果。
摘要由CSDN通过智能技术生成

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.测试结果

测试结果
我的个人博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhangz1z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值