2

I need to provide some kind activity protocol for unit tests to provide additional tracability to non technical actors...
So what i need is that every assertion also create a custom log entry.

Is there any way in AssertJ to achieve this? maybe some kind of listeners, extensions, ...

Currently the team uses plain JUnit 5 - I would really like to introduce AssertJ.

Actually the wanted behaviour is achieved via "overloading" JUnit Assertions.
So instead of Assertions.assertEquals(expected, actual) there is a method

MyAssertions.assertEqualsAndLog(Object expected, Object actual) { log(); //do the logging Assertions.assertThat(expected, actual) } 

But this is not really the way I want to do this.
Does anyone know a better way to achieve this?

thanks

1
  • Unit tests should be short. Configure the test framework to generate the report you need. Commented Dec 2, 2020 at 17:26

3 Answers 3

3

In AssertJ if you use assertion descriptions, you can print them or consume them with any consumer you have registered.

Example:

// initialize the description consumer final StringBuilder descriptionReportBuilder = new StringBuilder(String.format("Assertions:%n")); Consumer<Description> descriptionConsumer = desc -> descriptionReportBuilder.append(String.format("-- %s%n", desc)); // use the description consumer for any following assertions descriptions. Assertions.setDescriptionConsumer(descriptionConsumer); // execute some assertions TolkienCharacter frodo = new TolkienCharacter("Frodo", 33, Race.HOBBIT); assertThat(frodo.getName()).as("check name") .isEqualTo("Frodo"); assertThat(frodo.getAge()).as("check age") .isEqualTo(33); // get the report String descriptionReport = descriptionReportBuilder.toString(); 

will print this report:

Assertions: -- check name -- check age 

See https://assertj.github.io/doc/#assertj-core-assertion-description-consumer

Sign up to request clarification or add additional context in comments.

1 Comment

ahhhhhhh, descriptions.... this bugged the heck out of me. Thank you Joel, the library is fantastic.
0

Okay, unfortunately ther is no mechanism for events/hooks/etc (in combination with assertions). in AssertJ.
I found Issue#1518 which describes the same behavior.

Comments

0

The reason why tracking/counting/logging individual non-failing asserts is currently impossible is that the common contract of all(?) assertion libraries is very simple: Throw an AssertionError when failing, do nothing on success.

For JUnit seeing success events on assertion level would require a new way of coupling assertions to the framework. Given the very very slow uptake of opentest4j there’s probably never going to be an agreement among lib maintainers.

That said, any individual framework, like AssertJ, could provide a mechanism to react on success events. You just have to convince them to implement it - as the GitHub issue in the other answer shows.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.