Getting Started With JUnit 5 And Mockito For Unit Testing

Ruchira Shukla

Posted On: December 6, 2021

view count128666 Views

Read time12 Min Read

Mockito is a unit testing framework for Java that simplifies the task of automation testing. It makes unit testing highly effective with clean tests, thanks to dependency injection and compile-time checks. In addition, Mockito helps improve the test independence of components being developed by helping you create objects that have no dependencies on a test-specific configuration. The most popular way of using Mockito is within the JUnit framework to help you write better tests.

For starters, mocking in unit testing is used for isolating the AUT (Application Under Test) from external dependencies. Mocking must be leveraged when implementation of external dependencies is not yet completed.

In this JUnit 5 Mockito tutorial, we will see how Mockito can be used with the JUnit unit testing framework for mocking in JUnit 5. Furthermore, delve into an extensive compilation of commonly asked JUnit Interview Questions and Answers. This resource is designed to aid interview preparation and enhance your proficiency.

Let’s get started!

Getting started with Mockito and JUnit 5

Mockito is an open-source test automation framework that internally uses Java Reflection API to create mock objects. Mock objects are dummy objects used for actual implementation. The main purpose of using a dummy object is to simplify the development of a test by mocking external dependencies and using them in the code.

As per my experience, you should mock objects in unit tests when the real object has a non-deterministic behavior or the real object is a callback function or the real object is yet to be implemented.

In this JUnit 5 Mockito tutorial, we will use Mockito with JUnit for performing Selenium automation testing. In case you are starting with JUnit, look at this article on how to setup JUnit environment for your first test?. However, you can also go through the Selenium JUnit tutorial to check out the basics of JUnit, from writing your first test to running tests in Selenium with examples.

Also Read – What is Selenium?

Mockito Installation

When you use Mockito in your unit tests, you will need to download the jar file and place it in a path that your build system can find. Mockito is available in two versions: mockito-core (which contains only the core of Mockito, and mockito-all (which contains all modules).

The preferred way of installing Mockito is to declare a dependency on mockito-core with a build system of choice. The second best way is to download the artifacts using a manual approach and add them to the classpath. You can also add dependencies to your existing Maven or Gradle project.

Also Read: Run JUnit Tests From Command Line

Add the following dependencies in your pom.xml:

When using Gradle, this can be achieved by declaring the following dependency to your build.gradle:

The latest version of Mockito when writing this article is 4.1.0.

Annotations in Mockito

Like JUnit annotations, Mockito annotations are used to specify the behavior of the test code. It allows the users to focus more on their logic while still testing the code very effectively. This section of the JUnit 5 Mockito tutorial mainly focuses on Mockito annotations and how to use them in Selenium.

1. @Mock annotation in Mockito

Mock annotation is used to create a mock object.

The @Mock annotation is always used with @RunWith, a class-level annotation. We will see in detail how both annotations are used to create and use a mock object in the next segment.

For now, let’s see the following syntactic example:

2. @Spy annotation in Mockito

Spy annotation is used to create a real object and spy on that real object. This would help to call all the object methods while still tracking every interaction that is being mocked.

Below is the simple implementation of @Spy annotation:

3. @Captor annotation in Mockito

Captor annotation is used to create an ArgumentCaptor instance to capture method argument values for further assertions.

Here is a simple implementation of @Captor annotation that captures MyMap’s key and values:

Also read – JUnit Asserts With Examples

4. @InjectMocks annotation in Mockito

InjectMocks annotation is used to mock a class with all its dependencies. This is quite useful to test the behavior completely.

In the below example, we will use @InjectMock Countries into Continent:

Now that we have seen different Mockito annotations, let’s deep-dive into creating Mocks in the Mockito framework.

However, if you are intrigued to know more about JUnit Annotations in Selenium, you can go through the following video from the LambdaTest YouTube Channel and stay updated with more such videos on the JUnit tutorial with Selenium, Selenium Testing, Cypress Testing, and more.

How to create Mocks in Mockito?

In this section of the JUnit 5 Mockito tutorial, we will see different ways to create Mocks in the Mockito framework. In Mockito, we can create mock objects in two ways:

  1. Using @Mock annotation
  2. Using Mock() method

Using @Mock annotation to create Mocks

@Mock annotation minimizes repetitive mock objects and makes more readable test code and verification errors. It is available in the org.mockito package. Here is the code snippet of @mock annotation:

Using @Mock method to create Mocks

The Mockito.mock() method allows us to create a mock object of classes and interfaces. Here is the code snippet for a simple class mock:

In the above code snippet, MyList is a class for which we have created a mock object by passing that as a parameter in the mock() method. The second line sets an expectation. When the add() method of MyList class is called, it has to return false.

To understand this more clearly, let’s take an example of an eCommerce application. Let’s assume a scenario where you have to test the checkout feature. There is a class Order which has a method checkout() that finalizes the order and processes the payment. We are deliberately overlooking other conditions for the sake of simplicity.

So the Order class would look something like this:

In the above example, the checkout() method uses PaymentProviderService class for payment processing. The success or failure of checkout is based on the method processPayment().

So if we want to write test cases for checkout functionality, we also need to create an instance of the PaymentProviderService class. But we don’t want to depend on this class to start writing our unit tests. In this case, we would create a mock object of the PaymentProviderService class and continue writing tests for the Order class.

Here is the code snippet:

Code Walkthrough

I have created a test case for the Order class in the above code, which tests the successful order placement scenario.

The above line creates a mock object of the PaymentProviderService class. Now we can use this object to call the processPayment() method. We can write the expected behavior using when…thenReturn as:

After that, we can assert the expected and actual outputs and perform the desired function. Likewise, we have created 4 unit tests for the Order class. The output of those tests on running them on IDE is shown below:


GitHub Repository

How to use Mockito with JUnit 5 extensions?

This section of the JUnit 5 Mockito tutorial is devoted to the usage of Mockito with JUnit 5. It shows how to add a mock object to a test case and verify its behavior with JUnit 5. JUnit 5 has an extension model that supports Mockito out-of-the-box. The JUnit 5 extension model allows users to provide custom annotations and behaviors for test doubles.

To start with, JUnit Jupiter dependencies need to be added apart from mockito-core dependencies. So following dependencies have to be added in pom.xml(for Maven):

Here is how the dependencies can be added for Gradle:

The extension eliminates the need for the MockitoAnnotations.openMocks() method call. So the above example can be modified as:

It’s simple, isn’t it? This makes our code more readable and clean. Also, it can detect incorrect stubbing.

The next section of this JUnit 5 Mockito tutorial, will showcase parallel testing in Selenium using Mockito and JUnit.

This JUnit certification establishes testing standards for those who wish to advance their careers in Selenium automation testing with JUnit.

Here’s a short glimpse of the JUnit certification from LambdaTest:

How to perform parallel testing using Mockito and JUnit 5 on cloud Selenium Grid

JUnit 5 has the much-awaited capability of executing tests parallely, which significantly reduces the execution time. This is a life savior for testers as we have a huge no. of test cases in the real world. Leveraging this capability using an online Selenium Grid like LambdaTest would be more efficient.

Selenium testing tools like LambdaTest, allow you to perform cross browser testing on more than 3000 online browsers and operating system combinations.

Here is a quick video tutorial on the real-time browser testing.

In this section of the JUnit 5 Mockito tutorial, we will take a simple example to demonstrate parallel testing with Mockito and JUnit 5:

Problem Statement

  1. Navigate to https://www.lambdatest.com.
  2. Validate the page title is “Most Powerful Cross Browser Testing Tool Online | LambdaTest.”
  3. Enter username and password.
  4. Click on the login button.
  5. Validate successful login by “Welcome – LambdaTest” message.

We will be executing this test case on the following browsers, versions, and platform combinations using LambdaTest remote Selenium Grid:

Browser

Version

Platform

Chrome

70.0

WIN10

Safari

14.0.2

macOS Big Sur

Firefox

76.0

WIN10

Code Walkthrough

Three tests will run in parallel in the above code on the mentioned browser OS combinations.

The @Execution(ExecutionMode.CONCURRENT) annotation defines the parallel mode in JUnit 5. The LambdaTest remote Selenium Grid initiates the execution based on the arguments defined in the capabilities.

The @ParameterizedTest annotation defines that the annotated method is parameterized where the @MethodSource annotation is used to provide the method name, which is used as a source of parameter values.

Also read – JUnit Parameterized Test For Selenium Automation With Examples

At last, the browser() method, which is the method source, has the browser names as the arguments.

After execution, we will get logs as below:

As we can see, three tests are executed in parallel on three different browsers where they took around 60 seconds to execute.

We can also get the video of each run to find the issue if a test fails.

So, if you’re curious to know more about performing parallel testing with JUnit 5 and Selenium, this video will help you get started. This video will guide you through different parallel parameters in JUnit testing, including methods – execute test methods in separate threads and classes – to execute test classes in separate threads.

Conclusion

In this JUnit 5 Mockito tutorial, we have gone through the basics of the Mocking and Mockito framework. Also, what are different annotations in Mockito and their use cases. In JUnit 5, the creation of mocks using Mockito has been made more simple by extension. We have also discussed the parallel execution of test cases in JUnit5 using LambdaTest cloud Selenium Grid. Though Mockito is highly utilized in unit testing, we can also leverage its extensive capabilities in integrating those unit tests for Selenium automation testing.

How have you used Mockito in your testing? Let us know in the comments below.

Happy Testing!

Frequently Asked Questions

Does JUnit 5 work with Mockito?

Yes, Mockito provides an implementation for JUnit5 extensions in the library — mockito-junit-jupiter. The new version of the library makes it easier to mock abstract classes and use the verifyNoMoreInteractions() method that was not possible with the previous version of the library.

How do I use Mockito annotations?

Mockito annotations are mainly used to force JUnit to use the Mockito framework when running test cases. There are three ways of doing it:

  • Use @RunWith(MockitoJUnitRunner. class) at the top of the unit test class.
  • Use MockitoAnnotations. initMocks(this) in the @Before method of the unit test class.
  • Use MockitoJUnit. rule() to create MockitoRule class.

What is difference between JUnit and Mockito?

There is a huge difference between JUnit and Mockito. JUnit is more general, as it is related to unit tests in general (it doesn’t matter what you test). Mockito, instead, focuses on the mock objects and how to create them.

Both libraries are in Java, but the way of writing tests is different for both. For JUnit, you have to put your tests into separate classes that extend from junit.framework.TestCase. As for Mockito, you don’t write your tests inside a class, but in a method (just like normal methods).

The usage of Mockito is easier because you don’t need to set up and tear down the test environment every time you want to write a test – you can just write and run it. On the other hand, JUnit offers more options for setting up the environment (in order to make your test repeatable) if needed. And you are able to check what happens with those objects that were used inside the test case without any problems.

Author Profile Author Profile Author Profile

Author’s Profile

Ruchira Shukla

A software engineer by profession, an explorer by nature, and an avid reader. I enjoy writing and exploring technological developments.

Blogs: 6



linkedintwitter

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free