本文主要是介绍day26-单元测试,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1. 单元测试Junit
1.1 什么是单元测试?(掌握)
1.2 Junit的特点?(掌握)
1.3 基本用法:(掌握)
实际开发中单元测试的使用方式(掌握)
public class TestDemo {public int addMethod(int a,int b){return a+b;}
}
public class Main {@Testpublic void method(){TestDemo testDemo = new TestDemo();int result = testDemo.addMethod(3, 4);Assert.assertEquals("add方法错了",result,7);}
}
public class Main {@Beforepublic void beforeMethod() throws IOException {//先备份File src = new File("a.txt");File dest = new File("b.txt");FileInputStream fis = new FileInputStream(src);FileOutputStream fos = new FileOutputStream(dest);int b;while ((b=fis.read())!=-1){fos.write(b);}fos.close();fis.close();}@Testpublic void testMethod(){File file = new File("a.txt");//删除文件boolean result = file.delete();//文件是否存在boolean exists = file.exists();//只有同时满足了,才表示delete方法正确Assert.assertEquals("delete方法错了",result,true);Assert.assertEquals("delete方法错了",exists,false);}@Afterpublic void afterMethod() throws IOException {//还原数据File dest = new File("a.txt");File src = new File("b.txt");FileInputStream fis = new FileInputStream(src);FileOutputStream fos = new FileOutputStream(dest);int b;while ((b=fis.read())!=-1){fos.write(b);}fos.close();fis.close();//删除备份数据src.delete();}
}
这篇关于day26-单元测试的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!