0

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.

JeanValjean
  • 17,172
  • 23
  • 113
  • 157

1 Answers1

2

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

Joel Costigliola
  • 6,308
  • 27
  • 35
  • 1
    Hey thanks. Yes I realized that things were in that way at the end. I ended up with an implementation [like that one](https://github.com/ozimov/cirneco/blob/master/assertj/assertj-guava/src/main/java/it/ozimov/cirneco/assertj/JUnitGuavaEnhancedSoftAssertions.java). – JeanValjean Mar 20 '18 at 12:15
  • @Joel, I'd love a little hint on how to do this for `OptionalAssert`! – Matthew Simoneau Jan 14 '19 at 21:50
  • 1
    Try adding a method like the one for multimap: `public OptionalAssert assertThat(final Optional actual) { return proxy(OptionalAssert.class, Optional.class, actual); }` – Joel Costigliola Jan 15 '19 at 01:04