Mastering Selenium Testing: JUnit Asserts With Examples

Posted by Ramit Dhamija | October 3, 2019
Automation • Selenium Tutorial •

42218 Views | 14 Min Read

JUnit Asserts With Examples

Automation testing helps to ease the life of software testers by allowing them to automate their repetitive tasks, and open-source test automation frameworks like Selenium have enabled users to automate web testing experience, at scale. However, what good is automation testing if you are not able to verify whether a test case has passed or failed?

This is where assertions come into the picture, so you can keep a track of how many test failures or successes were encountered after the execution of your automation script for Selenium testing. Today, I am going to show you how to assert in JUnit, different types of assert in JUnit by examples.

We will be starting off with a basic introduction to assertions and JUnit. If you are already experiencing you may want to skip these headings. We would be talking in detail about the
following assert method for JUnit:

We would also glance upon the difference between JUnit4 and JUnit5 in terms of assertions in JUnit. Without further ado, here we go!

What Are Assertions? Why Are They Used?

Assertions are an integral part of the automation testing irrespective of the tools and frameworks put to use in Selenium testing. Assertions are used in the testing to verify or check that the result of the action/functionality is the same as what is expected after the execution of tests. Simply put, they are used to verify the status of a test case as either passed or failed.

When we run the test case/ scenario which we are automating, then figuring out the scenarios that passed or failed is pivotal to realize whether the execution of our automation script is as expected or not?

We have to provide some kind of assertions to do that, so at the end of the action, our code will compare and assert in JUnit or any other test automation framework, to evaluate If the result which we are getting is as expected.

If the actual results are the same as an expected result then we can mark that assertion as passed and if it does not meet then we can mark that assertion as failed.

When all the assertions in the test script are met, only then a test case is considered as passed. Assertions in Selenium Java can be handled with the predefined methods of JUnit framework.

There are 2 broad types of assertions for Selenium testing i.e. Hard Assertions, and Soft Assertions.

Hard Assertions – Hard assertions are used when we want out test script to halt immediately if the assertion conditions do not match the expected result. An Assertion Error will be encountered as the assertion condition failed to meet the expected outcome and the test case under execution will be marked as Failed.

Soft Assertions – The test script execution is not put to halt even if the assertion condition is not met. Also, in case of soft assertions, there will be no error thrown, when the assertion condition would fail to meet the expected result, and execution of test scripts will continue to the next test case step.

With that said, it is time to deep dive into various types of asserts in JUnit by examples.

Web UI Testing with Python and LambdaTest

Types Of Assertion In JUnit For Selenium Testing

Assert methods in JUnit are provided by the class “org.junit.Assert” which extends “java.lang.Object” class. Now, we will look into different methods to assert in JUnit by examples. If you are not familiar with JUnit you can refer to our blog: Automated Testing with JUnit and Selenium for Browser Compatibility.

assertEquals()

JUnit assertEquals() method compares equality of the expected result with the actual result. When the expected result provided by us does not match with the actual result of the Selenium testing script which we get after the action performed then it throws an assertion error. This leads to the termination of the execution of the test script, at that line itself.

Syntax:

Here is a JUnit assertEquals example to help you understand the process better.

In the above code, we can see that we have provided two parameters in the JUnit assertEquals() method, which is an expected result and actual result. If the value of actualURL does not match with the expected URL mentioned in the Selenium testing script then the assertion error will be thrown and execution of the program will get terminated at this same line i.e. assertion statement itself.

We can also pass assertion error message as a parameter which is shown in syntax.

JUnit assertEquals for Floating Point Assertion


If we require to compare floating point types (eg. double or float), in that case, to avoid errors of rounding we have to provide additional parameter which can be known as delta.
The value for delta can be evaluated as:

Math.abs(expected – actual) = delta

If there is any marginal difference in the expected and actual values due to rounding off, those can be considered as same and assertion should be marked as pass. So value of delta given by the user decides which margin value should be considered as fine to pass that assertion.

This can be used with float and double data types, please refer syntax below

Syntax:

Assert JUnit Example for Floating Point Assertion

assertTrue()

If you wish to pass the parameter value as True for a specific condition invoked in a method, then you can make use of the.JUnit assertTrue(). You can make use of JUnit assertTrue() in two practical scenarios.

  1. By passing condition as a boolean parameter used to assert in JUnit with the assertTrue method. It throws an AssertionError (without message) if the condition given in the method isn’t True.

    Syntax:

  2. By passing two parameters, simultaneously in the assertTrue() method. Wherein, one parameter would be for an assertion error message and the second parameter would be for specifying the particular condition against which we need to apply our assertion method as True. It throws an AssertionError (with message) if the condition given in the method is not True.

    Syntax:

Let us look at an assert JUnit example Selenium testing script for assertTrue():

In the above code, we can see that we have provided two parameters in the assertTrue() method, which is an assertion error message and boolean condition. If the condition does not match or is not true then the assertion error will be thrown and execution of the program will get terminated at this same line i.e. assertion statement itself.

If we do not want to provide assertion error message then we can just provide condition as we can see in the syntax mentioned above.

assertFalse()

On the contrary to JUnit assertTrue, we can make use of the assertFalse() method to verify whether a given condition is False or not. You can declare JUnit assertFalse in two ways as well.

  1. It takes a condition as a parameter against which the assertion needs to be applied.It throws an AssertionError (without message) if the condition given in the method is not False.

    Syntax:<>

  2. Similar to assertTrue, you can pass two parameters for assertFalse as well. One to determine the assertion error message and another to specify the condition against which apply the assertFalse. It throws an AssertionError (with message) if the condition given in the method is not False.

    Syntax:

Let us look at an assert JUnit example Selenium testing script for assertFalse():

In the above Selenium testing script, we can see that we have provided two parameters in the assertFalse() method, which is an assertion error message and a boolean condition. If the condition does match or is not false then the assertion error will be thrown and execution of the program will get terminated at this same line i.e. assertion statement itself.

If we do not want to provide assertion error message then we can just provide condition as we can see in the syntax mentioned above.

assertNull()

To verify whether a passed object contains a null value or not, we make use of the assertNull() method which helps to display an assertion error in case the object is not of null values.

Syntax:

Let us look at an example Selenium testing script for JUnit assertNull():

In the above code we can see that we have provided two parameters in the assertNull() method, which is an assertion error message and an object. If the provided object is not null then the assertion error will be thrown and the execution of the program will get terminated at this same line i.e. assertion statement itself.
If we do not want to provide assertion error message then we can just provide an object as we can see in the syntax mentioned above.

assertNotNull()

assertNotNull() method checks if provided object does not hold a null value. We can pass an object as a parameter in this method and we will get Assertion Error if the object which we pass does hold NULL values along with the assertion error message if provided.

Syntax:

Let us look at an assert JUnit example Selenium testing script for assertNotNull():

In the above code we can see that we have provided two parameters in the assertNotNull() method, which is an assertion error message and an object. If the provided object is null then only the assertion error will be thrown and the execution of the program will get terminated at this same line i.e. assertion statement itself.

If we do not want to provide an assertion error message then we can just provide an object as we can see in the syntax mentioned above.

assertSame()

While performing Selenium testing you may often encounter a scenario where you would need to compare two different objects passed as parameters in a method, to evaluate whether they are referring to the same object or not. This is where you can make use of JUnit assertSame(). As assertion error is displayed if the two objects don’t refer to the same object. Also, we will receive Assertion Error message along with that if message is provided as shown in below syntax.

Syntax for JUnit assertSame() in Selenium testing script:

assertNotSame()

assertNotSame() method verifies that if the two objects which we have passed as parameters are not equal. If both objects have the same references, then an Assertion Error will be thrown with the message which we have provided (if any).
One more thing to observe in this method is, it compares references of the object and not the value of those objects.

Syntax for JUnit assertNotSame() in Selenium testing script:

assertArrayEquals()

assertArrayEquals() method verifies that if the two object arrays which we have passed as parameters are equal. If both the object arrays have the values as null, then they will be considered as equal.
This method will throw an Assertion Error with the message provided if both the object arrays which we have passed as parameters in method are not considered equal.

Syntax for JUnit assertArrayEquals() in Selenium testing script:

Latest Version Of JUnit: JUnit 5

JUnit 5 offers a new architecture which is redesigned to overcome the problems with the older versions of JUnit. The new architecture consists of a combination of various modules for JUnit Platform, JUnit Jupiter and JUnit Vintage. If you are curious to know about JUnit Platform, JUnit Jupiter or JUnit Vintage then I suggest you read JUnit’s official guide.

JUnit 5 comes with some additional new methods to assert in JUnit. Before we go through them, we must realize the difference in ways to assert in JUnit 5 and JUnit 4.

Difference In JUnit Assertions Between JUnit5 vs JUnit4

JUnit Jupiter comes with many of the assertion methods which are already present in the JUnit 4 and it adds a few more assertion methods that lend themselves well to being used with Java 8 Lambdas. Ass the assertions in JUnit Jupiter are the static methods from the class org.junit.jupiter.api.Assertions

In Junit 4, org.junit.Assert has different assertion methods which validate expected results and resulted outcomes. Also we can give extra parameter for Assertion Error message as FIRST argument in method signature. You can refer to them in the below syntax or in every method which we discussed above.

Example:

In JUnit 5, org.junit.jupiter.Assertions contains most of assert methods including additional assertThrows() and assertAll() methods.

JUnit 5 assertions methods also have overloaded methods to support passing error message to be printed in case test fails

Junit 4
Junit 5
Class used is ‘org.junit.Assert’ Class used is ‘org.junit.jupiter.api.Assertions’
Assertion Error message is the first parameter, it is optional though Assertion Error message can be passed as the last parameter, it is also optional
New Methods: None New Methods: assertAll() and assertThrows()

Example:

New Methods To Assert In JUnit5

Now, that we have a clear understanding of the difference in ways to assert in JUnit5 vs JUnit 4. We will now dig deep into the latest methods to assert in JUnit5.

assertAll()

The newly added method assertAll() will be executed to check all the assertions as grouped assertion. It has an optional heading parameter which allows to recognize group of assertions with that method assertAll().While at the time of failure, assertion error message shows detailed information about each and every field assertion which are used in that group.

Let’s look at an assert JUnit example for assertAll with grouped assertion:

assertThrows()

Another newly added method in JUnit 5 is to replace the ExpectedException Rule from JUnit 4. Now, all assertions can be made against the returned instance of a class Throwable which will make the test scripts more readable. As executables we can use lambdas or method references.

Third-party Assertions In JUnit

JUnit Jupiter provides sufficient assertion facilities for most of the testing scenarios, but there can be some scenarios which require additional functionalities apart from which are provided by JUnit Jupiter like matchers are desired or required.
For such scenarios, it is recommended by JUnit team to use the third party assertion libraries like Hamcrest, AssertJ, Truth, etc. User can use these third-party libraries whenever there is a need.

Just for an assert JUnit example, To make assertions more descriptive and readable, we can use the combination of matchers and a fluent API.

While, JUnit Jupiter’s (JUnit 5) org.junit.jupiter.api.Assertions class does not provide method assertThat() which was available with org.junit.Assert class in JUnit 4 which accepts a Hamcrest Matcher. That way, you can leverage the built-in support for matchers offered by third party assertion libraries.

Assert JUnit Example for Selenium Testing using Third-Party assertions :

Results For Test Cases Passed In Selenium Testing:

  1. When the assertion is pass and so is test case, we can see the JUnit result as below
  2. junit

  3. When the assertion is failed and so is the test case, we can see that in the result as below. Also, we can see the Assertion Error message we have provided.
  4. assertion is failed

Summing It Up

Assertions play an indispensable role if you are performing automation with Selenium testing. They help us figure out whether a test case has passed or failed by evaluating the parameters passed into objects through our Selenium testing scripts. If you wish to have automation logs managed neatly and organized then assertions can do wonders for you.

So far you have learned various methods to assert in JUnit with examples, you also dwelled upon the difference between JUnit5 and JUnit4 with respect to assertions in JUnit. We also covered the newly introduced assertions in JUnit, along with third-party assertions in JUnit.

I hope this article helped you become proficient in Selenium testing with JUnit assertions. Let me know in the comments about how you use the assertions in JUnit, if there are any favourite ones, or if there are any questions. Happy Testing! 🙂

 cross browser testing

Written by Ramit Dhamija

Working as an Automation Tester at LambdaTest and has recently started the professional journey. Excels in Java test automation.

Related Articles

Leave a Reply