Java 为什么我的JUnit错误收集器没有报告错误?
我试图使用JUnit错误收集器报告错误。虽然我的断言失败了,但JUnit中并没有报告错误。但我在控制台中收到“错误”消息Java 为什么我的JUnit错误收集器没有报告错误?,java,junit,Java,Junit,我试图使用JUnit错误收集器报告错误。虽然我的断言失败了,但JUnit中并没有报告错误。但我在控制台中收到“错误”消息 @Rule public ErrorCollector errcol = new ErrorCollector(); @Then("^Business alert message on the screen$") public void Business_alert_message_on_the_screen(Result_Update) throws Throwable
@Rule
public ErrorCollector errcol = new ErrorCollector();
@Then("^Business alert message on the screen$")
public void Business_alert_message_on_the_screen(Result_Update) throws Throwable {
if (userType.equals("Admin")) {
try {
Assert.assertEquals("Update button is not present for the admin user", true, Result_Update);
} catch (Throwable t) {
errcol.addError(t);
System.out.println("Error");
}
}
}
据JUnit称:
ErrorCollector规则允许在测试结束后继续执行测试
第一个问题被发现了
这意味着在收集错误后测试将继续
您应该添加:
errcol.checkThat(...); //will pass/fail the test
见示例:
(更新)
cumber运行程序不支持
@Rule
,因为它扩展了ParentRunner
而不是BlockJUnit4ClassRunner
(请参阅)。已经有了一个新的解决方案
tl;dr:确保您的测试类不扩展TestCase
当我将JUnit4与IntelliJ IDEA一起使用时,我也遇到了类似的问题。我在对话框中天真地选择了一个基类TestCase
,这是JUnit3的默认值,因为我想“最好有那些方便的这个35; assert*
方法”(JUnit4的默认值为null)。错误代码(不起作用)如下所示:
public class SassCompilerTest extends TestCase {
@Rule
public ErrorCollector collector = new ErrorCollector();
@Test
public void testCompiler() throws IOException {
collector.checkThat(true, CoreMatchers.equalTo(false));
}
}
然而,在JUnit4中,这使许多功能无法工作。删除父类修复了测试:
public class SassCompilerTest {
@Rule
public ErrorCollector collector = new ErrorCollector();
@Test
public void testCompiler() throws IOException {
collector.checkThat(true, CoreMatchers.equalTo(false));
}
}
在另一个答案中,@StefanBirkner提到的黄瓜问题中,一位专家向我提出了解决方案。读了这篇文章后,我尝试扩展
ErrorCollector
,使ErrorCollector\verify
公开,并从@After
方法调用它,但是@After
方法没有被调用,这让我意识到TestRunner有问题(这是IntelliJ的默认设置)或者测试本身。您正在使用哪个运行程序(即用作@RunWith
参数的类)?@RunWith(Cucumber.class)好的,谢谢Lttiel。我无法理解的是为什么errcol.addError(t)只将错误添加到ErrorCollector,而没有使测试失败。据说加法器最终也会失败。我参考JUnit 4.12 JavaDoc。@AlexanderItes errcol.addError()收集测试流的所有错误,在errcol.checkthit()方法中出现测试失败/通过的情况。如果您需要进一步帮助,请共享您的代码。更新的文档和示例:在使用父类的情况下是否有任何解决方法?我必须保持抽象,但errorCollector不起作用。谢谢我知道了。对于cucumber(和父类),我无法使用errorCollector。有解决办法吗?谢谢
public class SassCompilerTest {
@Rule
public ErrorCollector collector = new ErrorCollector();
@Test
public void testCompiler() throws IOException {
collector.checkThat(true, CoreMatchers.equalTo(false));
}
}