Assertion in JUnit

The assertion in JUnit means “Trust but Verify” Assertion is performed to compare the Actual result with expected result. In JUnit assertion is performed with the help ofAssert Class.


There are a lot of methods in Assert class. Assert is basically a class which contains a lot of methods to validate the data by comparing two data, one is actual and other is expected. It contains a lot of overloaded methods.


Assertions are used to report the error in JUnit Report if assertion failed.  There are a lot of methods lets pick some important ones and understand these.


1. assertEquals(expected, actual) – This method is used to compare two objects in JUnit. If the assertion fails it will appear in the report.

Assertion in JUnit


You can see asserrtEquals() fails, so it throws an error in JUnit report. If the assertion fails it will execute the rest of the method after, where the assertion fails.


2. assertArrayEquals() – The assertArrayEquals() method will test whether two arrays are equal to each other. In other words, if the two arrays contain the same number of elements, and if all the elements in the array are equal to each other.

Assertion in JUnit


3. assertTrue() and assertFalse()– The assertTrue() and assertFalse() methods tests a single variable to see if its value is either true, orfalse. We also can use methods as arguments in the assertTrue() method.

Assertion in JUnit

So this is, How assertion helps us to compare the actual and expected in JUnit. My favorite is assertEquals(). We will see the actual use of JUnit in Selenium tutorials.


Selenium Tutorial:

If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.

0 Comment

Leave a Reply