Search code examples
groovyassert

Groovy Power Assert Statement Output


I'm using Spock and hate hard asserts for functional tests. I've written a SoftAssert class and everything works, I just want to make it look more like Groovy's power asserts.

Here is my existing method:

public void verifyEquals(def expected, def actual, String message = '') {
    try {
        assert expected == actual
    } catch (AssertionError e) {
        LOGGER.warn("Verification failed, expected: ${expected.toString()} but got ${actual}.\n ${message}")
        softAssertList.add(new SoftAssert(message, e))
    }
}

I'm just catching the failure, simple enough. Now what is lacking is the nice output that Groovy does so well. Instead of the statements I pass into verifyEquals such as: verifyEquals(8, (3 + x) * z)

8 == (3 + x) * z
  |     | |  | |
  |     | 0  | |
  |     3    | 2
  |          6
false

I get expected == actual

expected == actual
   |     |    |
   8     |    6
       false

This happens because the statement is calculated prior to passing it into the method. Does anyone know how to keep the test in the format of verifyEquals(8, (3 + x) * z) while passing in the parameters and how to keep the Power Assert error from the original parameters?


Solution

  • The verify method should accept a Closure (code block) containing the assertion, execute it, and store any exception thrown. Usage then looks as follows: verify { assert 8 == (3 + x) * z }.

    If you annotate the method with Spock's @ConditionBlock annotation and put it in a place where Spock can find it at compile time (e.g. base class), the assert keyword can even be omitted.

    Note that Spock's conditions aren't the same as Groovy's power asserts. If you catch Spock's ConditionNotSatisfiedError, you'll get further information about the assertion (e.g. a full syntax tree), which you can exploit to provide better feedback. If you don't need this additional information, then catching AssertionError is fine.