1、 Junit使用实例
1) 使用Junit和断言的方式实现简单的计算器。
操作步骤:
(1) 新建一个项目,导入Junit类包:Test_Of_Junit—>Build path—>Add Library—>Junit
(2)添加一个计算器类(Calcuate)
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.println("除数不能为0");
return 0;
}
else
return a/b;
}
}
2)创建一个Junit的测试类
(1) 新建一个Junit Test Case,内容如下
import org.junit.Assert;
import org.junit.jupiter.api.Test;
@SuppressWarnings("deprecation")
public class Test_case_of_Calculate {
@SuppressWarnings("deprecation")
@Test
void testAdd() {
Calculate test = new Calculate();
int result = test.add(3, 5);
Assert.assertEquals("加法有问题",8,result);
}
@Test
void testSubtract() {
Calculate test = new Calculate();
int result = test.subtract(12, 7);
Assert.assertEquals("减法有问题", 5,result);//注意,如果出现横线,说明导入包不正确
}
@Test
void testMultiply() {
Calculate test = new Calculate();
int result = test.multiply(5, 8);
Assert.assertEquals("乘法有问题", 40,result);
}
@Test
void testDevide() {
Calculate test = new Calculate();
int result = test.divide(60, 4);
Assert.assertEquals("除法有问题", 12, result);
}
}
3) 使用参数化实现计算器类Calculate的测试
import java.util.Arrays;
import java.util.Collection;
import org.junit.Assert;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
class Test_case1_of_Calculate {
@BeforeEach
public static void BeforeEach() {
System.out.println("***********进行测试***********");
}
@AfterAll
public static void AfterEach() {
System.out.println("***********测试结束***********");
}
@ParameterizedTest // 使用参数化运行
@MethodSource("test_way1") // 说明使用哪个方法用于参数化
@DisplayName("加法运算")
@Test
public void addTest(int x, int y, int z, String name) {
System.out.println("----------Test01----------" + name);
Calculate test = new Calculate();
int result = test.add(x, y);
Assert.assertEquals("加法有问题", z, result);
}
@ParameterizedTest // 使用参数化运行
@MethodSource("test_way2") // 说明使用哪个方法用于参数化
@DisplayName("减法运算")
@Test
public void subtractTest(int x, int y, int z, String name) {
System.out.println("----------Test02----------" + name);
Calculate test = new Calculate();
int result = test.subtract(x, y);
Assert.assertEquals("减法法有问题", z, result);
}
@ParameterizedTest // 使用参数化运行
@MethodSource("test_way3") // 说明使用哪个方法用于参数化
@DisplayName("乘法运算")
@Test
public void multiplyTest(int x, int y, int z, String name) {
System.out.println("----------Test03----------" + name);
Calculate test = new Calculate();
int result = test.multiply(x, y);
Assert.assertEquals("乘法法有问题", z, result);
}
@ParameterizedTest // 使用参数化运行
@MethodSource("test_way4") // 说明使用哪个方法用于参数化
@DisplayName("除法运算")
@Test
public void devideTest(int x, int y, int z, String name) {
System.out.println("----------Test04----------" + name);
Calculate test = new Calculate();
int result = test.divide(x, y);
Assert.assertEquals("除法有问题", z, result);
}
public static Collection<Object[]>test_way1() {// Collection集合类型,Object对象数组
return Arrays.asList(new Object[][] { { 3, 5, 9, "加法运算" }, { 4, 6, 10, "加法运算" } });
}
public static Collection<Object[]>test_way2() {// Collection集合类型,Object对象数组
return Arrays.asList(new Object[][] { { 9, 5, 4, "减法运算" }, { 24, 6, 10, "减法运算" } });
}
public static Collection<Object[]>test_way3() {// Collection集合类型,Object对象数组
return Arrays.asList(new Object[][] { { 3, 5, 15, "乘法运算" }, { 4, 6, 18, "乘法运算" } });
}
public static Collection<Object[]>test_way4() {// Collection集合类型,Object对象数组
return Arrays.asList(new Object[][] { { 8, 5, 2, "除法运算" }, { 4, 1, 4, "除法运算" } });
}
}