Junit测试:
代码如下:
package CalculatorPack;public class Calculator {public int add(int a,int b){return a+b;}public int sub(int a,int b){return a-b;}}
package CalculatorPack;import org.junit.Assert;
import org.junit.Test;public class CalculatorTest {@Testpublic void testAdd(){//1.创建计算机对象Calculator c = new Calculator();//2.调用add方法int res = c.add(1,2);
// System.out.println(res);//3.断言,我断言这个结果为3Assert.assertEquals(3,res);}@Testpublic void testSub(){Calculator c = new Calculator();int res = c.sub(1,2);Assert.assertEquals(-1,res);}}
测试结果:
代码如下:
package CalculatorPack;import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;import java.awt.*;public class CalculatorTest {/*初始化方法:用于资源申请,所有测试方法在执行之前都会先执行该方法*/@Beforepublic void init(){System.out.println("init...");}/*释放资源方法:在所有测试方法执行完后,都会自动执行该方法*/@Afterpublic void close(){System.out.println("close");}@Testpublic void testAdd(){System.out.println("add........");//1.创建计算机对象Calculator c = new Calculator();//2.调用add方法int res = c.add(1,2);
// System.out.println(res);//3.断言,我断言这个结果为3Assert.assertEquals(3,res);}@Testpublic void testSub(){System.out.println("sub....");Calculator c = new Calculator();int res = c.sub(1,2);Assert.assertEquals(-1,res);}}
测试结果: