AssertAll vs. multiple statements in JUnit5

Is there any reason to group multiple statements:

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

instead of this:

 public void shouldTellIfPrime(){ Assertions.assertTrue(isPrime(2)); Assertions.assertFalse(isPrime(4)); } 
+5
source share
2 answers

The interesting thing about assertAll is that it always checks all the statements passed to it , no matter how many failures. If everything goes well, everything is fine - if at least one of them fails, you will get a detailed result of everything that went wrong (and, for that matter).

It is best used to validate a set of properties that are conceptually combined. Something where your first instinct will be: "I want to assert this as one."

Example

Your specific example is not the best use case for assertAll , because checking isPrime using simple and non-simple is independent of each other - so much so that I would recommend writing two testing methods for this.

But suppose you have a simple class, such as an address with the city , street , number fields, and would like to state that this is what you expect from them:

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

Now, as soon as the first statement fails, you will never see the results of the second, which can be very annoying. There are many ways, and JUnit Jupiter assertAll is one of them:

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

If the test method returns an invalid address, this is an error:

 org.opentest4j.MultipleFailuresError: Should return address of Oracle headquarter (3 failures) expected: <Redwood Shores> but was: <Walldorf> expected: <Oracle Parkway> but was: <Dietmar-Hopp-Allee> expected: <500> but was: <16> 
+6
source

According to the documentation here

Claims that all provided executables do not raise an AssertionError.

If any of the attached executables throws an AssertionError, all other executables will still be executed, and all crashes will be aggregated and reported to MultipleFailuresError. However, if the executable generates an exception that is not an AssertionError, execution will be immediately terminated and the exception will be discarded as soon as it is masked as an exception.

Thus, the main difference is that assertAll will allow all statements to execute without breaking the thread, while others like assertTrue and the batch will stop the test using AssertionError

So, in your first example, both statements will be executed regardless of the failure path, and in the second example, the test will stop if the first statement fails.

Is there any reason to group multiple statements

If you want all statements to be executed in unit test.

+1
source

Source: https://habr.com/ru/post/1260319/


All Articles