Many automated testers use the JUnit Assert library to perform test validations.
When JUnit Assertion errors (test failures) occur, your test will stop executing and will not perform any remaining Assertions. For tests containing only a single Assertion, as is often the case, this is not an issue. But for tests that include multiple Assertions, you are limited in that your test will not run any Assertions after the first Assertion failure.
The problem becomes more pronounced the more Assertions your test contains. If you are validating hundreds of rows in an Excel sheet or a database table, it would make sense to see all of the Assertion failures at the end of the test execution, not just the first failure and then having to re-execute your test to see if there were any other failures one at a time. This is a limitation of vanilla JUnit Asserts. In this post, we will explore a solution to this problem.
Consider the following Cucumber Scenario example which validates the presence of multiple related fields on a UI:
Here is the code behind our example step definitions:
If we run the above Scenario, an Assertion error will be thrown for the presence of the username field:
As previously described, we are only seeing one Assertion failure: the first one. This test would fail all three of its validations, but we are unable to see this with vanilla JUnit Asserts.
To get around this limitation, I use a simple utility class that I wrote called "AggregatedAsserts". Below is the code for this class (feel free to adapt it as you see fit):
Using the AggregatedAsserts class is intentionally similar to using the Junit Assert library. This class includes methods that match the JUnit Assert functionality that I use the most, those methods being assertTrue(), assertFalse(), fail(), and assertNotNull(). If we were to re-write the above example Cucumber Scenario using AggregatedAsserts, it could look like this:
Example code for the steps:
Now, when we run the example Cucumber Scenario, the following is output to the console:
Instead of seeing only a single JUnit Assertion error, we now see of all our test's failures displayed in the console. The AggregatedAsserts utility class is all that I used accomplish this. I recommend using the AggregatedAsserts class for tests that have multiple Assertion statements so the entirety of the test's results can be viewed when the execution is completed.
Jared Hoernemann is a Senior Automation Engineer specializing in designing and coding test automation scripts and frameworks from the ground up, as well as mentoring other developers. Jared designs, implements, and executes automated tests for web browsers, APIs of all kinds, and databases.
Have a QA question?
Our team would love to help!