在使用 JUnit 4.x 提過,JUnitCore是個 Facade 類別,會自動判別應該使用哪個Runner來執行測試。如果你從JUnitCore的main()開始執行,則測試結果會顯示在文字模式下,如果你想要自訂一個結果輸出,也可以使用JUnitCore。
JUnitCore有個靜態(static)的runClasses()方法,接受一系列的測試類別:
public static Result runClasses(Class<?>... classes)
使用這個runClass()版本,會傳回一個Result物件,代表著測試執行的結果,例如一些可取得的資訊為:
public int getRunCount()
public int getFailureCount()
public long getRunTime()
public List<Failure> getFailures()
public int getIgnoreCount()
public int getFailureCount()
public long getRunTime()
public List<Failure> getFailures()
public int getIgnoreCount()
其中getFailures()傳回的List中包括Failure,代表一些測試失敗的資訊。一個簡單的使用範例如下:
package cc.openhome;
import org.junit.*;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
import static org.junit.Assert.*;
public class CalculatorTest {
private Calculator calculator;
@Before
public void setUp() {
calculator = new Calculator();
}
@After
public void tearDown() {
calculator = null;
}
@Test
public void testPlus() {
int expected = 1;
int result = calculator.plus(3, 2);
assertEquals(expected, result);
}
@Test
public void testMinus() {
int expected = 1;
int result = calculator.minus(3, 2);
assertEquals(expected, result);
}
public static void main(String[] args) {
Result result = JUnitCore.runClasses(CalculatorTest.class);
for(Failure failure : result.getFailures()) {
System.out.print(failure.getTestHeader() +
": " + failure.getMessage());
}
}
}
如果你對於測試過程中的啟始、結束、失敗等有興趣,則可以繼承RunListener:
public class RunListener {
// 所有測試開始之前
public void testRunStarted(Description description) throws Exception {}
// 所有測試結束之後
public void testRunFinished(Result result) throws Exception {}
// 某個測試開始前
public void testStarted(Description description) throws Exception {}
// 某個測試開始後
public void testFinished(Description description) throws Exception {}
// 某個測試失敗時
public void testFailure(Failure failure) throws Exception {}
// 某個預期失敗的測試
public void testAssumptionFailure(Failure failure) {}
// 某個測試忽略時
public void testIgnored(Description description) throws Exception {}
}
// 所有測試開始之前
public void testRunStarted(Description description) throws Exception {}
// 所有測試結束之後
public void testRunFinished(Result result) throws Exception {}
// 某個測試開始前
public void testStarted(Description description) throws Exception {}
// 某個測試開始後
public void testFinished(Description description) throws Exception {}
// 某個測試失敗時
public void testFailure(Failure failure) throws Exception {}
// 某個預期失敗的測試
public void testAssumptionFailure(Failure failure) {}
// 某個測試忽略時
public void testIgnored(Description description) throws Exception {}
}
JUniCore有個addListener(),可以讓你在建立JUnitCore實例,添加自己的RunListener。例如:
JUnitCore junitCore = new JUnitCore();
junitCore.addListener(new RunListener() {
// 一些感興趣的方法實作
public void testStarted(Description description) throws Exception {
System.out.println(description.getDisplayName() + "...started");
}
});
Result result = junitCore.run(CalculatorTest.class);
for(Failure failure : result.getFailures()) {
System.out.print(failure.getTestHeader() +
": " + failure.getMessage());
}
附帶一題的是,JUnitCore在內部,會建立Computer的實例,傳給Request的classes()方法,Request的classes()方法會建立預設的RunnerBuiilder(AllDefaultPossibilitiesBuilder),使用Computer()實例的getSuite()方法,自動判斷該傳回哪個Runner實例,其尋找Runner實例的方式可以稍微了解一下:
public Runner runnerForClass(Class<?> testClass) throws Throwable {
List<RunnerBuilder> builders= Arrays.asList(
ignoredBuilder(),
annotatedBuilder(),
suiteMethodBuilder(),
junit3Builder(),
junit4Builder());
for (RunnerBuilder each : builders) {
Runner runner= each.safeRunnerForClass(testClass);
if (runner != null)
return runner;
}
return null;
}
protected JUnit4Builder junit4Builder() {
return new JUnit4Builder();
}
protected JUnit3Builder junit3Builder() {
return new JUnit3Builder();
}
protected AnnotatedBuilder annotatedBuilder() {
return new AnnotatedBuilder(this);
}
protected IgnoredBuilder ignoredBuilder() {
return new IgnoredBuilder();
}
protected RunnerBuilder suiteMethodBuilder() {
if (fCanUseSuiteMethod)
return new SuiteMethodBuilder();
return new NullBuilder();
}