1. 程式人生 > >Junit官方入門指南

Junit官方入門指南

This small example shows you how to write a unit test. You need to have a JDK installed and a text editor. (In general it is recommended to use a build tool for building your software and running the tests.)

一個簡單的展示你如何去寫一個單元測試。你需要安裝一個JDK和一個文字編輯器(比較推薦使用一個構建工具,用於構建你的軟體和執行的測試)


Preparation 準備工作

Create a new folder junit-example and download the current junit-4.XX.jar from JUnit’s release page and Hamcrest to this folder.

建立一個檔案 junit-example 並且下載 junit相關的jar包剛在該目錄下,下載地址

Change to the folder junit-example. All files are created within this folder and all commands are executed there, too.

當改變junit-example檔案時,這個資料夾裡所有建立的檔案和所有命令也都會被執行。


Create the class under test 建立一個類,用來測試

Create a new file Calculator.java and copy the following code to this file.

建立一個檔案 Calculator.java,並且拷貝如下程式碼到該檔案中

public class Calculator {
  public int evaluate(String expression) {
    int sum = 0;
    for (String summand: expression.split("\\+"))
      sum += Integer.valueOf(summand);
    return sum;
  }
}

Now compile this class: 編譯這個類檔案

javac Calculator.java

The Java compiler creates a file Calculator.class.

這個java 編輯器建立了一個檔案 Calculator.class.


Create a test 建立一個測試

Create the file CalculatorTest.java and copy the following code to this file.

建立一個檔案 CalculatorTest.java 並且複製下面程式碼到檔案中

import static org.junit.Assert.assertEquals;
import org.junit.Test;

public class CalculatorTest {
  @Test
  public void evaluatesExpression() {
    Calculator calculator = new Calculator();
    int sum = calculator.evaluate("1+2+3");
    assertEquals(6, sum);
  }
}

Compile the test. On Linux or MacOS

編譯這個test在Linux 或者 MackOS上

javac -cp .:junit-4.XX.jar alculatorTest.java

and on Windows

在windows上

javac -cp .;junit-4.XX.jar alculatorTest.java

The Java compiler creates a file >CalculatorTest.class.
java編譯器建立一個檔案 CalculatorTest.class.

.
Run the test 執行測試

Run the test from the command line. On Linux or MacOS

在 Linux 或者 Macos 環境下,從命令列中執行test

java -cp .:junit-4.XX.jar:hamcrest-core-1.3.jar org.junit.runner.JUnitCore CalculatorTest

nd on Windows

在windows環境下

java -cp .;junit-4.XX.jar;hamcrest-core-1.3.jar org.junit.runner.JUnitCore CalculatorTest

The output is

輸出結果

JUnit version 4.12
.
Time: 0,006

OK (1 test)

The single . means that one test has been run and the OK in the last line tells you that your test is successful.

這一個 . 的意思是,這個測試已經被執行,並且成功執行到最後一行,告訴你你的測試時成功的。


Let the test fail 測試失敗

Modify Calculator.java in order to get a failing test. Replace the line

修改 Calculator.java 為了獲得一個錯誤的測試,替換這一行

sum += Integer.valueOf(summand);

with 用這個替換

sum -= Integer.valueOf(summand);

and recompile the class.

並且重新編譯類檔案

javac Calculator.java

Run the test again. On Linux or MacOS

重複執行測試,在linux 或者Macos上

java -cp .:junit-4.XX.jar:hamcrest-core-1.3.jar org.junit.runner.JUnitCore CalculatorTest

and on Windows

在windows 上

java -cp .;junit-4.XX.jar;hamcrest-core-1.3.jar org.junit.runner.JUnitCore CalculatorTest

Now the test fails and the output is

現在測試失敗,並且輸出

JUnit version 4.12
.E
Time: 0,007
There was 1 failure:
1) evaluatesExpression(CalculatorTest)
java.lang.AssertionError: expected:<6> but was:<-6>
  at org.junit.Assert.fail(Assert.java:88)
  ...

FAILURES!!!
Tests run: 1,  Failures: 1

JUnit tells you which test failed (evaluatesExpression(CalculatorTest)) and what went wrong:

Junit 告訴你那個測試失敗,evaluatesExpression(CalculatorTest) 和 什麼出錯了

java.lang.AssertionError: expected:<6> but was:<-6>

意思是,斷言出錯了,,應該是6,,但是結果卻是-6