Search code examples
javaguavajunit4assertj

AssertJ JUnitSoftAssertions and Guava assertions


I'm addicted to the AssertJ JUnit rule JUnitSoftAssertions. It is really conveniente, you just add it as a test class field

@Rule
public JUnitSoftAssertions softy = new JUnitSoftAssertions();

and you chain several assertThat on it.

Now, I added the dependency for Guava assertions from AssertJ, but looks to me that there is no rule or no way to register the new assertions in the JUnit rule. Hence I must use ugly static imports.

Am I wrong? If so, please explain how to use them in a JUnit rule (without implementing it by myself.


Solution

  • There is no soft assertions support in assertj-guava at the moment but adding it is not too difficult, it only requires a class like:

    /**
     * A single entry point for all soft assertions, AssertJ standard assertions and MyProject custom assertions.
     */
    // extending make all standard AssertJ assertions available
    public class GuavaJUnitSoftAssertions extends JUnitSoftAssertions {
    
      public <K, V> MultimapAssert<K, V> assertThat(final Multimap<K, V> actual) {
        return proxy(MultimapAssert.class, Multimap.class, actual);
      }
    
      // add the other guava assertThat methods 
      // ... 
    }
    

    Happy to get a contribution for that (I'm a bit busy at the moment).

    Hope it helps