有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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 {
    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");
        }
     }
}

共 (3) 个答案

  1. # 2 楼答案

    tl;dr : Make sure your test class doesn't extend TestCase.

    当我将JUnit4与IntelliJ IDEA一起使用时,我也遇到了类似的问题。我在对话框中天真地选择了一个基类TestCase,这是JUnit 3的默认值,因为我想“如果有那些方便的this#assert*方法就好了”(JUnit 4的默认值为空)。错误代码(不起作用)如下:

    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在另一个答案中提到的黄瓜问题中的一位comment向我提出的。读了这篇文章后,我尝试扩展ErrorCollector使ErrorCollector#verify成为公共的,并从@After方法调用它,但是@After方法没有被调用,这让我意识到TestRunner(这是IntelliJ的默认设置)或测试本身有问题