3

I would like to provide the same textual message for a group of assertions, something like

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

@Test
public void myTest() {

  MyClass result = ...
  withClue("the result {} does not conform", result.toString()) {
     assertThat(result.id, notEmpty())
     assertThat(result.xxxx, hasLength(33))
  }

}

My expectation is that the "clue" will be shown before the first failed assertion. In Scala I'm used to ScalaTest withClue() that I usually use to show the full text representation of object (usually a JsonNode in my case) which usually allows me to understand better what went wrong.

So is there any way with vanilla JUnit, hamcrest of any other library dependency to get a message prepended to a group of assertions?

RubenLaguna
  • 21,435
  • 13
  • 113
  • 151

0 Answers0