2

I'm using AssertJ to test web using fluentlenium and extent reports for reporting the results.

I asked before the question but forgot to mention the use of AssertJ.

The provided answer was to extend soft assert and that it has onAssertFailure function.

Is there anything like this for AssertJ soft assertions? Or is there another solution to bypass it?

Ryan Payne
  • 5,249
  • 4
  • 28
  • 69
A.H
  • 23
  • 5
  • Which assertj softassert are you using? There are many different endpoints, hard to know which to try and extend. – Mobrockers Jun 02 '16 at 06:41
  • I know there is softassertion class from there example. I'm not fully understand what you are saying – A.H Jun 02 '16 at 06:49
  • These are all the SoftAssertions variants assertj offers: - [AutoCloseableBDDSoftAssertions](https://github.com/joel-costigliola/assertj-core/blob/master/src/main/java/org/assertj/core/api/AutoCloseableBDDSoftAssertions.java) - [AutoCloseableSoftAssertions](https://github.com/joel-costigliola/assertj-core/blob/master/src/main/java/org/assertj/core/api/AutoCloseableSoftAssertions.java) - [BDDSoftAssertions](https://github.com/joel-costigliola/assertj-core/blob/master/src/main/java/org/assertj/core/api/BDDSoftAssertions.java) – Mobrockers Jun 02 '16 at 06:56
  • - [JUnitBDDSoftAssertions](https://github.com/joel-costigliola/assertj-core/blob/master/src/main/java/org/assertj/core/api/JUnitBDDSoftAssertions.java) - [JUnitSoftAssertions](https://github.com/joel-costigliola/assertj-core/blob/master/src/main/java/org/assertj/core/api/JUnitSoftAssertions.java) - [SoftAssertions](https://github.com/joel-costigliola/assertj-core/blob/master/src/main/java/org/assertj/core/api/SoftAssertions.java) – Mobrockers Jun 02 '16 at 06:57
  • SoftAssertions the basic one – A.H Jun 02 '16 at 07:04

2 Answers2

2

In the next AssertJ version (2.5.0) you will have access to all soft assertions errors (see this commit).

Hope it helps

Joel Costigliola
  • 6,308
  • 27
  • 35
  • This could definitely be used by OP for what he wanted. However I think the testng solution of providing hooks for failure/success events is very nice. Do you think you might see a use for that in your framework? – Mobrockers Jul 15 '16 at 08:42
  • AssertJ being community driven, if a user asks for it with a concrete use case, yes. – Joel Costigliola Jul 16 '16 at 04:34
1

In a future release of assertJ a method wasSuccess() is added (as can be seen on git history), but it is not yet available in the current release.

When this method is added you can do something like this:

public class AssertjSoftAssert extends SoftAssertions {

    private void checkFailure() {

        if(!wasSuccess()) {

            onFailure();
        }
    }

    private void onFailure() {

        //doFailureStuff
    }

    @Override
    public BigDecimalAssert assertThat(BigDecimal actual) {

        BigDecimalAssert assertion = super.assertThat(actual);
        checkFailure();
        return assertion;
    }

    @Override
    public BooleanAssert assertThat(boolean actual) {

        BooleanAssert assertion = super.assertThat(actual);
        checkFailure();
        return assertion;
    }
}

Do note, however, that you will have to override EVERY assertion method in the SoftAssertions class like I've shown you with the examples here. And also if new Assertions are added to the SoftAssertions class you will have to override those as well. This is the best solution I could find right now, but won't work until assertj is updated either.

EDIT: Actually I am not sure this would even work because I am not sure wasSuccess() will return true after every successvul softassert or only after throwing assertAll() but I can't test this obviously as the feature isn't out yet.

Bonus: The commit that added wasSuccess()

Mobrockers
  • 2,128
  • 1
  • 16
  • 28
  • 1
    `wasSuccess` returns the result of the latest (soft) assertion so that you can decide what assertion to perform next. See the [javadoc](http://joel-costigliola.github.io/assertj/core-8/api/org/assertj/core/api/AbstractSoftAssertions.html#wasSuccess--) – Joel Costigliola Jul 15 '16 at 08:26