Writing Unit Tests in Java with Mockito and JUnit

Touseef Khan
3 min readMar 29, 2024

Unit testing is a critical part of software development. It helps us ensure that our code behaves as expected and makes it easier to maintain and refactor our code with confidence. In this blog post, we’ll explore how to write unit tests in Java using two popular libraries: Mockito and JUnit.

What are Mockito and JUnit?

JUnit is a simple, open-source framework to write repeatable tests in Java. It is used to write and run repeatable automated tests, to ensure your code runs as intended.

Mockito is a mocking framework for Java. It simplifies the process of creating mock objects, making it easier to test interactions between the class under test and its dependencies.

Setting Up

First, you need to add the Mockito and JUnit dependencies to your project. If you’re using Maven, add the following to your pom.xml:

<dependencies>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.12.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>

Writing a Simple Test with JUnit

Let’s start by writing a simple test with JUnit. Suppose we have a Calculator class with an add method:

package com.coding.touseef;


public class Calculator {
public int add(int a, int b) {
return a + b;
}
}

We can write a test for the add method like this:

package com.coding.touseef;

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

public class CalculatorTest {
@Test
public void testAdd() {
Calculator calculator = new Calculator();
int result = calculator.add(2, 3);
assertEquals(5, result);
}
}

In this test, we’re creating an instance of Calculator, calling the add method, and then using the assertEquals method from JUnit to check that the result is as expected.

Using Mockito to Mock Dependencies

Now, let’s see how we can use Mockito to mock dependencies. Suppose our Calculator class depends on a RandomNumberGenerator class:

package com.coding.touseef;


public class Calculator {
private RandomNumberGenerator rng;

public Calculator(RandomNumberGenerator rng) {
this.rng = rng;
}

public int addRandomNumber(int a) {
int b = rng.generate();
return a + b;
}
}

Mockito provides several annotations, such as @Mock, that make it easier to create mock objects into your tests We can use it to create a mock RandomNumberGenerator for our test:

package com.coding.touseef;

import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class CalculatorTest {
@Mock
private RandomNumberGenerator rng;

@Test
public void testAddRandomNumber() {
MockitoAnnotations.openMocks(this);
Mockito.when(rng.generate()).thenReturn(3);

Calculator calculator = new Calculator(rng);
int result = calculator.addRandomNumber(2);

assertEquals(5, result);
}
}

In this test, we’re creating a mock RandomNumberGenerator using the @Mock annotation. We then specify that when the generate method is called, it should return 3. Finally, we create a Calculator with our mock RandomNumberGenerator and test the addRandomNumber method.

Conclusion

Mockito and JUnit are powerful tools for writing unit tests in Java. With JUnit, we can easily write tests to check that our code behaves as expected. With Mockito, we can create mock objects to isolate the code under test from its dependencies. Together, they make it easier to write tests that are simple, clear, and reliable.

I hope this blog post has helped you understand how to use Mockito and JUnit for writing unit tests in Java. Happy testing!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Touseef Khan
Touseef Khan

Written by Touseef Khan

Experienced full-stack developer proficient in Java, AEM, & .NET. Let's connect: https://www.linkedin.com/in/touseef-khan-19214930/

Responses (1)

Write a response

Is there a reason why you are using an old version of mockito ? (https://central.sonatype.com/artifact/org.mockito/mockito-core/versions) why not using the BOM file of mockito? also better use most recent version of JUnit Jupiter and also the BOM of…