标签:Exception IntelliJ int void 单元测试 IDEA throws Test public
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
- 自动生成的测试类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.测试结果
标签:Exception,IntelliJ,int,void,单元测试,IDEA,throws,Test,public 来源: https://blog.csdn.net/qq_48519116/article/details/120774583
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。