Skip to content

Assertions

Assertions are instructions, which are used to confirm, that the objects acquired in tests fulfil certain assumptions and verify the correctness of a test that was run. Assertions are static methods from org.junit.jupiter.api.Assertions class, which allow the comparison of the actual method results with the expected ones. Their call should be found at the end of the test, ie. in then section.

Basic assertions

JUnit offers various assertions, each of them has many overloads, and some of them are ie.:

  • assertEquals(expected, actual) - compares objects with use of the equals method. The expected value should always be the first argument
  • assertNotNull(object) - checks if the given object does not equal null.
  • assertNull(object) - checks if the given object is equal to null.
  • assertTrue(value) - expects, that the boolean value transferred as an argument equals true.
  • assertFalse(value) - similar as above, expects that the argument equals false.
  • assertSame(o1, o2) - expects, that the references are the same (ie. compares objects with ==).
  • assertNotsame(o1, o2) - checks if the references are different.
  • assertFail() - which immediately ends the test with failure. Is used for example to test exceptions with try catch method.

Advanced assertions

Above asertions can be found in JUnit version 4 and 5. Version 5 introduces several others, ie.:

  • assertArrayEquals(expectedArray, actualArray) - checks equality and order of all element in both tables.
  • assertIterableEquals(expectedIterable, actualIterable) - checks equality and order of all element in objects implementing the interface Iterable. This interface is implemented by the interface Collection, therefore this assertion can be used with any collection.
  • assertLinesMatch(expected, actual) - checks, with use of an algorithm and possibly regular expressions, two String lists contan the same elements.
  • assertTimeout(timeout, executable) - checks, if the code is executed quicker, than the value of the timeout argument. The task has to be finished before we proceed to the next line.
  • assertTimeoutPreemptively() - works similarly to assertTimeout, but the task executable will not be finished in case timeout will be achieved.

Each assertion can be overloaded by adding a new parameterr with it's own message, which will be displayed in case when such assertion will finish with a failure.

Grouping assertions

Test in the then section can perform any amount of assertions. In our case, when there will be several and one of them will finish with a failure, the next assertions will not be performed. If we want, all of the assertions to be always performed , we have to execute them inside of the assertion assertAll(...). This assertion uses varargs, expecting an Executable type of object. This object can be implemented with use of lambdywith no arguments, np.:

assertAll(
  () -> assertEquals(expectedObject, actualObject, "Assertion 1 failed"),
  () -> assertTrue(actualResult, "Assertion 2 failed")
);

In the example above, if both assertions will finish with a failure, then we will see the following message:

Assertion 1 failed
Assertion 2 failed

Examples using assertions can be found here. These examples also use additional references.