从JUnit测试中调用一个创建失败的方法

时间:2013-01-18 18:59:37

标签: java selenium junit automation

从我的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();
}

}

2 个答案:

答案 0 :(得分:0)

我想你没有用@Rule注释收藏家。它应该是ErrorCollector。 API doc中有一个例子:

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.
    }
}

希望这有帮助

答案 1 :(得分:0)

修正了此问题。

我没有动
@Rule public ErrorCollector collector= new ErrorCollector();

线。