Unit testing is a crucial aspect of software development that helps ensure code quality and functionality. In this interactive blog post, we’ll explore the basics of unit testing in Java using two powerful frameworks: JUnit and Mockito. By the end, you’ll have a solid understanding of how to write and run unit tests effectively.
Unit Testing is a software testing method where individual units or components of a software application are tested in isolation from the rest of the application. The main objectives of unit testing include:
To begin, ensure you have the following tools installed:
If you’re using Maven, add the following dependencies to your pom.xml
:
Xml code
<dependencies>
<dependency>
<groupId>junit
</groupId>
<artifactId>junit
</artifactId>
<version>4.13.2
</version>
<scope>test
</scope>
</dependency>
<dependency>
<groupId>org.mockito
</groupId>
<artifactId>mockito-core
</artifactId>
<version>4.0.0
</version>
<scope>test
</scope>
</dependency></dependencies>
If you’re using Gradle, add the following to your build.gradle
:
Groovy code
dependencies {
testImplementation 'junit:junit:4.13.2'
testImplementation 'org.mockito:mockito-core:4.0.0'
}
Let’s start by writing a simple Java class that we will test.
Example Class: Calculator.java
Java code
public
class
Calculator {
public
int
add(int a, int b) {
return a + b;
}
}
Now, we’ll create a test class to test the Calculator
class.
Test Class: CalculatorTest.java
java code
import org.junit.Assert;
import org.junit.Test;
public
class
CalculatorTest {
@Test
public
void
testAdd() {
Calculator
calculator
=
new
Calculator();
Assert.assertEquals(
5, calculator.add(
2,
3));
}
}
Using IDE: Most IDEs have built-in support for running JUnit tests. Right-click on the test class and select “Run.”
Using Command Line: If you’re using Maven, you can run the tests using:
Bash code
mvn
test
Mockito is used to create mock objects for dependencies in your classes, allowing you to isolate the unit you are testing.
Example Class with Dependency: UserService.java
Java code
public
class
UserService {
private UserRepository userRepository;
public
UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
public User
getUserById(int id) {
return userRepository.findById(id);
}
}
Mocking with Mockito: UserServiceTest.java
Java code
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
public
class
UserServiceTest {
@Test
public
void
testGetUserById() {
UserRepository
mockRepository
= Mockito.mock(UserRepository.class);
UserService
userService
=
new
UserService(mockRepository);
User
user
=
new
User(
1,
“John Doe”);
Mockito.when(mockRepository.findById(
1)).thenReturn(user);
Assert.assertEquals(user, userService.getUserById(
1));
}
}
@Test
: Marks a method as a test method.@Before
: Runs before each test method.@After
: Runs after each test method.@BeforeClass
: Runs once before all test methods.@AfterClass
: Runs once after all test methods.Mockito.mock(ClassName.class)
Mockito.when()
to define the behavior of mocks.Mockito.verify()
to check if methods were called.Unit testing is a fundamental practice that improves code reliability and maintainability. With JUnit and Mockito, you can write effective unit tests that validate your code’s behavior. By following the examples and best practices outlined in this guide, you’ll be well on your way to mastering unit testing in Java.
To reinforce your understanding, try answering the following questions:
What is the purpose of unit testing?
Which annotation is used to mark a test method in JUnit?
What does Mockito allow you to do?
(Answers: 1-b, 2-b, 3-b)
Feel free to comment below if you have any questions or if you’d like to share your experiences with unit testing in Java!
Comments are closed