注意:MyTest 的代码已更新以反映我找到的答案,这是由 JUnit 生态系统中可用的多个 assertEquals() 实现引起的。
我在 C 中看到过类似的问题,但没有答案/响应。我要么在实现上左转,要么忽略了一些简单的事情。所以...
我希望在控制台上看到“IT FAILED”和“IT PASSED”,但并不高兴。只有“testFailure()”和“testSuccess()”输出。
TestRunner 实现
import junit.runner.Version; // added to support version check
import org.junit.runner.JUnitCore;
public class TestRunner {
public static void main(String[] args) {
System.out.println(Version.id()); // added to check JUnit version
JUnitCore.runClasses(MyTest.class);
}
}
测试实现
// NO LONGER NEDED (v3 -v- v4) import junit.framework.TestCase;
import org.junit.Rule;
import org.junit.Test;
import ort.junit.Assert.assertEquals; // version that invokes TestWatcher
// public class MyTest extends TestCase { // JUnit v3.X semantics
public class MyTest { // JUnit v4.X semantics
@Rule
public MyTestWatcher myTestWatcher = new MyTestWatcher();
@Test
public void testFailure() {
System.out.println("testFailure()");
assertEquals("failure", true, false);
}
@Test
public void testSuccess() {
System.out.println("testSuccess()");
assertEquals("failure", true, true);
}
}
TestWatcher 实现
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
public class MyTestWatcher extends TestWatcher {
public MyTestWatcher() {}
@Override
protected void failed(Throwable e, Description description) {
System.out.println("IT FAILED");
}
@Override
protected void succeeded(Description description) {
System.out.println("IT PASSED");
}
}