0

我是测试新手,我必须assertj使用softassertions. 这些是在多个测试中重复的标准断言。对于我定义的每个测试 new SoftAssertion,做断言然后做.assertAll()

这似乎是很多样板代码。是否可以抽象assertall()基类中的断言函数和方法,以便我的测试可以扩展该类?

4

2 回答 2

0

我知道这个问题已经很老了。但是我来这里是为了寻找答案,我自己制定了答案。希望这个答案对其他人有所帮助。SoftAssertion 可以通过以下方式使用 ThreadLocal 变量抽象为 BaseClass,

public class BaseTest {

    ThreadLocal<SoftAssert> softAssert = new ThreadLocal<SoftAssert>();

    @BeforeMethod
    public void initialise() {
        softAssert.set(new SoftAssert());
    }
}

public class AppTest extends BaseTest{
    @Test(alwaysRun = true)
    public void test1() throws Exception {
        softAssert.get().fail("test1() I got failed - Test1");
        softAssert.get().fail("test1() I got failed - Test1");
        softAssert.get().assertAll();
    }
    
    @Test(alwaysRun = true)
    public void test2() {
        softAssert.get().fail("test2 I got failed - Test2");
        softAssert.get().fail("test2 I got failed - Test2");
        softAssert.get().assertAll();
    }
}

**

Output:
Test1 output:
java.lang.AssertionError: The following asserts failed:
    test1() I got failed - Test1,
    test1() I got failed - Test1
Test2 Output:
java.lang.AssertionError: The following asserts failed:
    test2 I got failed - Test2,
    test2 I got failed - Test2

**

于 2022-02-21T03:32:53.890 回答
0

您可以使用JUnitSoftAssertions如下所示:http: //joel-costigliola.github.io/assertj/assertj-core-features-highlight.html#soft-assertions

于 2016-07-22T07:38:23.420 回答