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();}
}