以下是我的测试文件:
package test;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
public class TestWatcherTest
{
@Rule
public TestWatcher testWatcher = new TestWatcher()
{
protected void failed(Throwable e, Description description)
{
System.out.println("in failed()");
}
};
@Test
public void shouldFail()
{
Assert.assertTrue("Test failed", false);
}
}
输出为:
<failure message="Test failed" type="junit.framework.AssertionFailedError">junit.framework.AssertionFailedError: Test failed at test.TestWatcherTest.shouldFail(TestWatcherTest.java:24)
</failure>
似乎failed()没有被执行。我读了很多帖子,但似乎没有什么对我有用的。我做错了什么?
问题出在ant配置上。从…起http://testautomationusingjunit.blogspot.com/2013/08/applying-rules-to-junit-tests-running.html,需要编辑目标以在类路径中包含JUnitCore。
<target name="junit" depends="build">
<java classname="org.junit.runner.JUnitCore">
<classpath>
<path location="selenium_server/selenium-server-standalone-xxx.xx.jar"/>
</classpath>
</java>
<junit fork="no" haltonfailure="no" printsummary="true" failureProperty="test.failed" dir=".">
<test name="src.SampleTest" todir="./report" />
</junit>
</target>