Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

assertAll vs multiple assertions in JUnit5

Is there any reason to group multiple assertions:

public void shouldTellIfPrime(){     Assertions.assertAll(             () -> assertTrue(isPrime(2)),             () -> assertFalse(isPrime(4))     ); } 

instead of doing this:

public void shouldTellIfPrime(){     Assertions.assertTrue(isPrime(2));     Assertions.assertFalse(isPrime(4)); } 
like image 715
Wilhelm Olejnik Avatar asked Nov 25 '16 02:11

Wilhelm Olejnik


People also ask

Why do we use assertAll?

assertAll validates all test cases. If some assertions fail, then also it will continue the rest of the assertions and return the validation result for all failed assertion.

What is assertion in junit5?

Assertions is a collection of utility methods that support asserting conditions in tests. Unless otherwise noted, a failed assertion will throw an AssertionFailedError or a subclass thereof.

What is assertAll?

The assertAll() method when collating assertion failures uses the Soft Assert class object. It will collate all the assertion failures for the same object at a single time.

How do you group assertions together in JUnit?

Grouped Assertions With Heading As Parameter Here is an example where assertEquals () and assertIterableEquals () are grouped together using the method assertAll (). It consists of the heading parameter with the value “GroupedAssertionHeading”. Result : As both asserts are passed, the final result passes.


2 Answers

The interesting thing about assertAll is that it always checks all of the assertions that are passed to it, no matter how many fail. If all pass, all is fine - if at least one fails you get a detailed result of all that went wrong (and right for that matter).

It is best used for asserting a set of properties that belong together conceptionally. Something where your first instinct would be, "I want to assert this as one".

Example

Your specific example is not an optimal use case for assertAll because checking isPrime with a prime and a non-prime is independent of each other - so much so that I would recommend writing two test methods for that.

But assume you have a simple class like an address with fields city, street, number and would like to assert that those are what you expect them to be:

Address address = unitUnderTest.methodUnderTest(); assertEquals("Redwood Shores", address.getCity()); assertEquals("Oracle Parkway", address.getStreet()); assertEquals("500", address.getNumber()); 

Now, as soon as the first assertion fails, you will never see the results of the second, which can be quite annoying. There are many ways around this and JUnit Jupiter's assertAll is one of them:

Address address = unitUnderTest.methodUnderTest(); assertAll("Should return address of Oracle's headquarter",     () -> assertEquals("Redwood Shores", address.getCity()),     () -> assertEquals("Oracle Parkway", address.getStreet()),     () -> assertEquals("500", address.getNumber()) ); 

If the method under test returns the wrong address, this is the error you get:

org.opentest4j.MultipleFailuresError:     Should return address of Oracle's headquarter (3 failures)     expected: <Redwood Shores> but was: <Walldorf>     expected: <Oracle Parkway> but was: <Dietmar-Hopp-Allee>     expected: <500> but was: <16> 
like image 119
Nicolai Parlog Avatar answered Oct 14 '22 09:10

Nicolai Parlog


According to documentation here

Asserts that all supplied executables do not throw an AssertionError.

If any supplied Executable throws an AssertionError, all remaining executables will still be executed, and all failures will be aggregated and reported in a MultipleFailuresError. However, if an executable throws an exception that is not an AssertionError, execution will halt immediately, and the exception will be rethrown as is but masked as an unchecked exception.

So main difference is that the assertAll will allow all the asserts to execute without breaking the flow while the others like assertTrue and the lot will stop the test with the AssertionError

So in your first example both assertions will execute regardless of pass to fail, while in the second example test will stop if first assertion fails.

Is there any reason to group multiple assertions

If you want all assertions exercised in the unit test.

like image 21
Nkosi Avatar answered Oct 14 '22 10:10

Nkosi