Junit
本次使用Junit4直接导jar包 maven工程pom.xml 引入也可以
package com.yang.test;
//1.新建项目2.导入jar包3.新建待测类4.创建测试类5.运行测试类
public class Calculator {
public int add(int a,int b) {
return a+b;
}
public int subtract(int a,int b) {
return a-b;
}
public int mutiply(int a,int b) {
return a*b;
}
public int divide(int a,int b) {
return a/b;
}
}
package com.yang.test;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
//成功测试类
public class CalculatorTest {
@Test
public void testAdd() {
Calculator c=new Calculator();
assertEquals(7,c.add(3, 4));//预期7
}
@Test
public void testSubtract() {
Calculator c=new Calculator();
assertEquals(1,c.subtract(4, 3));//预期1
}
@Test
public void testMutiply() {
Calculator c=new Calculator();
assertEquals(12,c.mutiply(3, 4));//预期12
}
@Test
public void testDivide() {
Calculator c=new Calculator();
assertEquals(1,c.divide(3,3));//预期1
}
}
package com.yang.test;
import static org.junit.Assert.*;
import org.junit.Test;
//失败测试类
public class ErrorCalculatorTest {
@Test
public void testAdd() {
Calculator c=new Calculator();
assertEquals(6,c.add(3, 4));//预期7
}
@Test
public void testSubtract() {
Calculator c=new Calculator();
assertEquals(2,c.subtract(4, 3));//预期1
}
@Test
public void testMutiply() {
Calculator c=new Calculator();
assertEquals(10,c.mutiply(3, 4));//预期12
}
@Test
public void testDivide() {
Calculator c=new Calculator();
assertEquals(1,c.divide(3,0));//被除数不能为0
}
}