根據以前的條件來對後續的結果進行預判。java
開啓步驟:單元測試
assert判斷條件;或者assert判斷條件:提示信息;測試
當項目調試完畢,直接手動將斷言關閉便可。用做代碼調試很是方便。spa
示例以下:調試
public static void main(String[] args) { System.out.println("請輸入一個大於10的數:"); Scanner scan = new Scanner(System.in); int n = scan.nextInt(); n +=5; assert n > 15:"須要一個大於10的值"; n *= 3; System.out.println(n); }
單元測試是編寫測試代碼,用來檢測特定的、明確的、細顆粒的功能。單元測試並不必定保證程序功能是正確的,更不保證總體業務是準備的。code
a、要求被測試的方法沒有參數blog
b、要求被測試的方法沒有返回值 ---返回值類型必須是void資源
c、要求被測試的方法必須是非靜態方法 it
5. 單元測試方法既能夠多個執行也能夠鼠標右鍵單個執行。io
java單元測試示例:
public class JunitDemo { //1. 測試須要初始化的方法 FileWriter writer; //對於多個方法執行前要執行的方法加上該註解,就能夠提早執行 @Before public void init() throws IOException{ //true表示容許追加 writer = new FileWriter("D:\\test.txt",true); } @Test public void writerHello() throws IOException { writer.write("hello"); } @Test public void writeJava() throws IOException { writer.write("java"); } @After public void close() throws IOException { writer.close(); } //2. 測試無參方法 @Test public void m() { System.out.println(10 /0); System.out.println("running~~`"); } public int sum(int i, int j) { return i + j; } public double sum(double i, double j) { return i + j; } //3. 測試帶參數方法的方式 @Test public void test() { System.out.println(sum(2,3)); System.out.println(sum(2.0,3.0)); } }
總結:以上就是除了Debug以外,經常使用的兩種java調試方法。