DEV Community

Cover image for Mastering Selenium Testing: JUnit Asserts With Examples
ramitd1995
ramitd1995

Posted on • Originally published at lambdatest.com

Mastering Selenium Testing: 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 forSelenium 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:

  1. assertEquals()
  2. assertTrue()
  3. assertFalse()
  4. assertNull()
  5. assertNotNull()
  6. assertSame()
  7. assertNotSame()
  8. assertArrayEquals()
  9. assertAll()
  10. assertThrows()
  11. Third-Party Assertions in 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.

Assert.assertEquals(String expected, String actual);
      Assert.assertEquals(String message, String expected, String actual); 

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

package com.test;

import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class AssertionDemoClass {


    public static WebDriver driver;

    @BeforeClass
    public static void openBrowser()
    {
        System. setProperty("webdriver.chrome.driver", "D:\\AutomationPractice\\chromedriver_win32\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
    }

    @Test
    public void assertURL() {
        // TODO Auto-generated method stub
        driver.get("https://www.lambdatest.com/");

        String actualURL = driver.getCurrentUrl();
        System.out.println(actualURL);

        Assert.assertEquals("https://www.lambdatest.com/",actualURL);
        System.out.println("Test Passed");
    }

    @AfterClass
    public static void closeBrowser()
    {
        driver.close();
    }

} 

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:

public static void assertEquals(float expected,float actual,float delta)
public static void assertEquals(double expected,double actual,double delta) 

Assert JUnit Example for Floating Point Assertion

@Test
    public void assertURL() {
        // TODO Auto-generated method stub      

        double actualDoubleValue= 2.999;
        double expectedDoubleValue = 3.000;

        Assert.assertEquals(expectedDoubleValue, actualDoubleValue, 0.001);

        System.out.println("Test Passed");      

    } 

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:

Assert.assertTrue(boolean assertCondition); 
  1. 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:

Assert.assertTrue(String message, boolean assertCondition); 

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

package com.assertions.junit 1;

import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class AssertionDemo1 {

    public static WebDriver driver;

    @BeforeClass
    public static void openBrowser()
    {
        System. setProperty("webdriver.chrome.driver", "D:\\AutomationPractice\\chromedriver_win32\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
    }

    @Test
    public void assertURL() {
        driver.get("https://www.spicejet.com/");

        String actualURL = driver.getCurrentUrl();
        System.out.println(actualURL);

        String expectedURL="https://www.spicejet.com/";

        Assert.assertTrue("URL does not match",expectedURL.equals(actualURL));
        System.out.println("Test Passed");  

    }


    @AfterClass
    public static void closeBrowser()
    {
        driver.close();
    }
} 

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:<>

Assert.assertFalse(boolean condition); 
  1. 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:

Assert.assertFalse(String message, boolean condition);

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

package com.assertions.junit 1;

import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class AssertionDemo1 {

    public static WebDriver driver;

    @BeforeClass
    public static void openBrowser()
    {
        System. setProperty("webdriver.chrome.driver", "D:\\AutomationPractice\\chromedriver_win32\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
    }

    @Test
    public void assertURL() {
        // TODO Auto-generated method stub
        driver.get("https://www.spicejet.com/");

        String actualURL = driver.getCurrentUrl();
        System.out.println(actualURL);

        String expectedURL="https://www.google.com/";

        Assert.assertFalse("URL does match",expectedURL.equals(actualURL));
        System.out.println("Test Passed");          
    }

    @AfterClass
    public static void closeBrowser()
    {
        driver.close();
    }
} 

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:

Assert.assertNotNull(Object obj);
    Assert.assertNotNull(String msg, Object obj);

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

package com.assertions.junit 1;

import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class AssertionDemo1 {

    public static WebDriver driver;


    //To open the browser before running test
    @BeforeClass
    public static void openBrowser()
    {
        System. setProperty("webdriver.chrome.driver", "D:\\AutomationPractice\\chromedriver_win32\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
    }

    @Test
    public void assertURL() {

        //Redirect to the URL 
        driver.get("https://www.spicejet.com/");

        String actualURL = driver.getCurrentUrl();
        System.out.println(actualURL);

        String expectedURL="https://www.google.com/";

        //Assertion     
        Assert.assertNotNull("Null Object", actualURL);;
        System.out.println("Test Passed");  

    }

    //To close the browser after running test
    @AfterClass
    public static void closeBrowser()
    {
        driver.close();
    }

} 

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:

Assert.assertNotNull(Object obj);
    Assert.assertNotNull(String msg, Object obj); 

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

package com.assertions.junit 1;

import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class AssertionDemo1 {

    public static WebDriver driver;


    //To open the browser before running test
    @BeforeClass
    public static void openBrowser()
    {
        System. setProperty("webdriver.chrome.driver", "D:\\AutomationPractice\\chromedriver_win32\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
    }

    @Test
    public void assertURL() {

        //Redirect to the URL 
        driver.get("https://www.spicejet.com/");

        String actualURL = driver.getCurrentUrl();
        System.out.println(actualURL);

        String expectedURL="https://www.google.com/";

        //Assertion     
        Assert.assertNotNull("Null Object", actualURL);;
        System.out.println("Test Passed");  

    }

    //To close the browser after running test
    @AfterClass
    public static void closeBrowser()
    {
        driver.close();
    }

} 

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:

 a) Assert.assertSame(Object expected, Object actual);
    b) Assert.assertSame(String message, Object expected, Object actual); 

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:

   a) Assert.assertNotSame(Object expected, Object actual);
    b) Assert.assertNotSame(String message, Object expected, Object actual); 

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:

a) Assert.assertArrayEquals(Object[] expected, Object[] actual);
    b) Assert.assertArrayEquals(String message, Object[] expected, Object[] actual);

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:

public static void assertEquals(long expected, long actual);
    public static void assertEquals(String message, long expected, long actual); 

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

Capture35.PNG

Example:

public static void assertEquals(long expected, long actual);
public static void assertEquals(long expected, long actual, String message);
public static void assertEquals(long expected, long actual, Supplier messageSupplier); 

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:

package com.test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;

import java.util.Arrays;
import java.util.List;

import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class AssertionDemoClass {

    public static WebDriver driver;

    @BeforeClass
    public static void openBrowser()
    {
        System. setProperty("webdriver.chrome.driver", "D:\\AutomationPractice\\chromedriver_win32\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
    }

    @Test
    public void assertURL() {
        // TODO Auto-generated method stub
        driver.get("https://www.spicejet.com/");


        String actualURL = driver.getCurrentUrl();
        System.out.println(actualURL);
        String expectedURL="https://www.spicejet.com/";

        String actualTitle =  driver.getTitle();
        System.out.println(actualTitle);
        String expectedTitle = "SpiceJet - Flight Booking for Domestic and International, Cheap Air Tickets";

        Assertions.assertAll( 
                () -> assertEquals(expectedURL, actualURL),
                () -> assertEquals(expectedTitle, actualTitle)
                );

        System.out.println("Test Passed");
    }

    @AfterClass
    public static void closeBrowser()
    {
        driver.close();
    }
}

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 :

@Test
    public void assertWithHamcrestMatcher() {       

        TestAssertClass tA = new TestAssertClass();

        assertThat(tA.testSubstract(5, 2), is(equalTo(3)));                 
    } 

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

junit

  1. 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.

assertion is failed

Summing It Up

Assertions play an indispensable role if you are performingautomation 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

Top comments (0)