Assert Methods

Learn about a variety of assert methods used to test if the actual output matches the expected output.

There are a number of assert methods that are available with the import of the Assertions.* package.

assertEquals()

The assertEquals() method compares two values — the expected value and the actual value — to determine whether or not both are the same. A test method from the last lesson using assertEquals() is reproduced here:

Press + to interact
@Test
public void testfindIndex_numberNotInArray() {
ArrayMethods arrayMethods = new ArrayMethods();
assertEquals(-1, arrayMethods.findIndex(new int[]{8,4,5}, 1));
}

There are many variations of assertEquals() with different data types. An optional third String argument can be added that contains a message about what the test is for. This feature is handy for a code base spanning thousands of lines, where the developer might not know what a failing test intended to do. We will change the ...

Access this course and 1400+ top-rated courses and projects.