0

For example, for this assertion -

aString = "stack overflow"; another string = "stack";

assertThat(aString).startsWith(anotherString);

is it possible to add a logger like this -

assertThat(aString ["stack overflow"]).startsWith(another strong ["stack"])

..or something similar. I'm interested in logging the type of assertions and the name and current values of fields under assertion implicitly.

2 Answers2

1

You should read the code of AssertJ (for your example, that would be AbstractStringAssert, AbstractCharSequenceAssert and AbstractAssert) and you'll have your answer: this is not possible in AssertJ directly. Even if, in case of failure, the error should clearly indicate the assertion being tested.

You could however do it in other ways:

  • Have code coverage enabled on AssertJ, that would give at least the class/method of AssertJ that were used in your test (but you wan't have the values).
  • Play with ASM or Byte code manipulation to do the trick yourself.
  • Fork AssertJ repository and add your own logging™: you could hack your way in, by looking at what is done when an assertion fail and add an additional log after the failure case.
NoDataFound
  • 11,381
  • 33
  • 59
0

As NoDataFound pointed out, there is no logging of assertions out of the box in AssertJ, adding such a mechanism is being discussed here https://github.com/joel-costigliola/assertj-core/issues/1518.

Joel Costigliola
  • 6,308
  • 27
  • 35