-1

从我的 JUnit 测试中我想调用这个方法,它允许我调用它,但是即使它应该导致错误它通过测试而不是失败。

public void handleErrors(String string, boolean b) {
    System.out.println(string + ", " + b);
    if(b == false){
        collector.addError(new Throwable(string + ", " + b));
    }   
}

如果我通过这个应该会导致测试失败,它应该会失败,但不会。

@Test
public void test() throws InterruptedException {
    handleErrors("Button was found", false);
}

为什么 JUint 不报告失败?

编辑:

package com.selenium.tests;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.openqa.selenium.By;
import org.junit.rules.ErrorCollector;
import com.selenium.AbstractScreenTest;
public class test1 extends AbstractScreenTest{

@Rule
public ErrorCollector collector= new ErrorCollector();

@Before
public void initialize() {
createTest();
}

@Test
public void test() throws InterruptedException {
handleErrors("Button was found", false);
}

public void handleErrors(String string, boolean b) {
System.out.println(string + ", " + b);
if(b == false){
collector.addError(new Throwable(string + ", " + b));
}   
}

@After
public void tearDown(){
closeTest();
}

}
4

2 回答 2

0

我想你没有用@Rule 注释收集器。它应该是ErrorCollector。API文档中有一个示例:

public static class UsesErrorCollectorTwice {
    @Rule
    public ErrorCollector collector= new ErrorCollector();

    @Test
    public void example() {
            collector.addError(new Throwable("first thing went wrong"));
            collector.addError(new Throwable("second thing went wrong"));
            collector.checkThat(getResult(), not(containsString("ERROR!")));
            // all lines will run, and then a combined failure logged at the end.
    }
}

希望这可以帮助

于 2013-01-18T19:05:16.693 回答
0

修复了这个。我没有移动

@Rule public ErrorCollector collector= new ErrorCollector();

线。

于 2013-01-18T19:22:42.247 回答