0

I am new to testing and I have to use assertj framework for softassertions. These are standard assertions that are repeated over multiple tests. For every test I define new SoftAssertion, do the asserts and then do .assertAll()

This seems like a lot of boiler plate code. Is it possible to abstract the assert functions and the assertall() method in a base class so that my tests could extend the class?

piyushj
  • 1,546
  • 5
  • 21
  • 29
LameDuck31
  • 21
  • 3
  • It depends on what you are testing. - If you are testing one feature, but with different input/output, then maybe you want to look at parametised testing (something like JUnitParams) - If you are testing multiple classes with common behaviour (usually extend the same class), you can create a base test class for those tests to extend from. Do you have any example code? – jimmygchen Jul 22 '16 at 05:41
  • Sorry if my question is not clear. I am currently using simple Softassertions to assert strings and values. These tests by themselves are not very complex. However I have a lot of tests and in every file I define a new soft assert, perform the assertion and then call the assertall method. I was wondering if this part of the tests the can be abstracted in a base class in the before and after methods. – LameDuck31 Jul 22 '16 at 17:29

2 Answers2

0

You can use JUnitSoftAssertions as shown here : http://joel-costigliola.github.io/assertj/assertj-core-features-highlight.html#soft-assertions

Joel Costigliola
  • 6,308
  • 27
  • 35
0

I know this question is pretty old. But I came here searching for an answer and I formulated the answer myself. Hope this answer helps someone else. SoftAssertion can be abstracted to the BaseClass in the following way using ThreadLocal variable,

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

**

Kishore Mohanavelu
  • 439
  • 1
  • 6
  • 17