pilegic
pilegic

Reputation: 1

is it possible to log the name and values of fields being asserted along with conditions when using assertj?

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.

Upvotes: 0

Views: 377

Answers (2)

Joel Costigliola
Joel Costigliola

Reputation: 7076

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.

Upvotes: 0

NoDataFound
NoDataFound

Reputation: 11959

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.

Upvotes: 1

Related Questions