您好,欢迎访问三七文档
当前位置:首页 > 中学教育 > 初中教育 > Junit单元测试实验报告
实验二Junit单元测试实验报告实验内容:利用Junit对实验程序Point2d.java进行单元测试实验目的:掌握单元测试的方法,掌握在Eclipse里进行Junit测试的技术。实验步骤和结果:1、修改之前的Calculator的测试结果:(1)自动生成的CalculatorTest类代码:packageandycpp;publicclassCalculator{privatestaticintresult;//静态变量,用于存储运行结果publicvoidadd(intn){result=result+n;}publicvoidsubstract(intn){result=result-1;//Bug:正确的应该是result=result-n}publicvoidmultiply(intn){}//此方法尚未写好publicvoiddivide(intn){result=result/n;}publicvoidsquare(intn){result=n*n;}publicvoidsquareRoot(intn){for(;;);//Bug:死循环}publicvoidclear(){//将结果清零result=0;}publicintgetResult(){returnresult;}}(2)运行结果:自动生存的测试类完善测试类后的运行结果2、修改和完善Calculator类:packageandycpp;publicclassCalculator{privatestaticintresult;//静态变量,用于存储运行结果publicvoidadd(intn){result=result+n;}publicvoidsubstract(intn){result=result-n;//Bug:正确的应该是result=result-n}publicvoidmultiply(intn){result=result*n;}publicvoiddivide(intn){result=result/n;}publicvoidsquare(intn){result=n*n;}publicvoidsquareRoot(intn){result=(int)Math.sqrt(n);//Bug:死循环}publicvoidclear(){//将结果清零result=0;}publicintgetResult(){returnresult;}}3、修改后的Calculator的测试用例:(1)加法:操作数:2,、3;结果:5(2)减法:操作数:10、2;结果:8(3)乘法:操作数:10、2;结果:20(4)除法:操作数:8、2;结果:4(5)平方:操作数:3;结果:9(6)开方:操作数:4;结果:2代码:packageandycpp;importstaticorg.junit.Assert.*;importorg.junit.Before;importorg.junit.Ignore;importorg.junit.Test;publicclassCalculatorTest{privatestaticCalculatorcalculator=newCalculator();@BeforepublicvoidsetUp()throwsException{calculator.clear();}@TestpublicvoidtestAdd(){calculator.add(2);calculator.add(3);assertEquals(5,calculator.getResult());}@TestpublicvoidtestSubstract(){calculator.add(10);calculator.substract(2);assertEquals(8,calculator.getResult());}@TestpublicvoidtestMultiply(){calculator.add(10);calculator.multiply(2);assertEquals(20,calculator.getResult());}@TestpublicvoidtestDivide(){calculator.add(8);calculator.divide(2);assertEquals(4,calculator.getResult());}@TestpublicvoidtestSquare(){calculator.square(3);assertEquals(9,calculator.getResult());}@TestpublicvoidsquareRoot(){calculator.squareRoot(4);assertEquals(2,calculator.getResult());}}4、修改后的Calculator的测试结果:结果正确5、Point2d的测试用例:测试用例1:pt1=(0.0,0.0)pt2=(4.0,3.0)importstaticorg.junit.Assert.*;importorg.junit.Before;importorg.junit.Test;publicclassPoint2dTest{privatestaticPoint2dpt1=newPoint2d();privatestaticPoint2dpt2=newPoint2d(4.0,3.0);@BeforepublicvoidsetUp()throwsException{}@TestpublicvoidtestDistanceFrom(){assertEquals(5,pt1.distanceFrom(pt2),0.1);}@TestpublicvoidtestDistanceFromOrigin(){assertEquals(5,pt2.distanceFromOrigin(),0.1);}}6、Point2d的测试结果:两个测试用例结果均正确
本文标题:Junit单元测试实验报告
链接地址:https://www.777doc.com/doc-4638925 .html