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 theequals
method. The expected value should always be the first argumentassertNotNull(object)
- checks if the given object does not equalnull
.assertNull(object)
- checks if the given object is equal tonull
.assertTrue(value)
- expects, that theboolean
value transferred as an argument equalstrue
.assertFalse(value)
- similar as above, expects that the argument equalsfalse
.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 withtry 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 interfaceIterable
. This interface is implemented by the interfaceCollection
, 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 thetimeout
argument. The task has to be finished before we proceed to the next line.assertTimeoutPreemptively()
- works similarly toassertTimeout
, but the taskexecutable
will not be finished in casetimeout
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.