如何定义最后一个测试方法断言将被涉及

时间:2016-02-11 11:51:03

标签: junit

我有一个用于测试回归测试的两个类。在某些情况下,我们在类和方法中有多个测试方法,我们通常使用断言。我想知道是否有任何方法可用,只能使用@Rule测试方法中的最后一个方法。这是我的代码:

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class JustOneClass extends ParentClass {

    @Rule
    public class GeneralRule articleHotspotRule = new class GeneralRule (this);

    @Test
    aMethod(){
        Assert.assertTrue()
    }
    @Test
    bMethod(){
        Assert.assertTrue()
    }
    @Test
    cMethod(){
        Assert.assertTrue()
    }
    @Test
    dMethod(){
        if this assert is failed Assert.assertTrue()
    }
}

我们有另一个扩展TestWatcher

的类
public class GeneralRule extends TestWatcher {

    private ParentClass baseTest;

    public GeneralRule (final GeneralRule generalRule) {
        this.baseTest = generalRule;
    }

    @Override
    protected void failed(final Throwable e, final Description description) {
        baseTest.after();
    }

}

在这种情况下,我希望只有在baseTest.after()的断言失败时才会使用dMedthod

1 个答案:

答案 0 :(得分:1)

而不是使用规则来尝试检查失败,如何检查失败条件然后以编程方式使测试失败?当然不是一般的优雅或可重复使用,但可能满足您的要求。

@Test
public void dMethod() {
    ...
    if(actual == false) {             // check for failure scenario
        after();                      // call the after method
        Assert.fail("hello failure"); // programatically fail the test
    }
}
相关问题