Testing in Java: Frameworks and Tools (JUnit, TestNG, Mockito)

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Testing in Java: Overview of Frameworks and Tools



Quality testing is an integral part of Java development. Modern frameworks allow you to automate code verification, detect errors at early stages, and ensure application stability. In this article, we will review key tools for unit testing, mocking, and integration testing in Java, and provide practical examples of their use.



1. JUnit 5 — The Standard for Unit Testing



JUnit 5 (Jupiter) is the most popular framework for modular testing in Java. It provides annotations for defining test methods, managing the lifecycle, and parameterization.



Key JUnit 5 Annotations:

  • @Test — marks a test method
  • @BeforeEach / @AfterEach — executed before/after each test
  • @BeforeAll / @AfterAll — executed once for the entire class
  • @ParameterizedTest — for tests with different input data


Example of a simple test:

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

public class CalculatorTest {

@Test void testAddition() { Calculator calc = new Calculator(); assertEquals(5, calc.add(2, 3)); }

@Test void testDivisionByZero() { Calculator calc = new Calculator(); assertThrows(ArithmeticException.class, () -> calc.divide(10, 0)); }}


2. TestNG — An Alternative with Extended Capabilities



TestNG (Test Next Generation) is another powerful framework, inspired by JUnit, but offering additional features: test grouping, dependent tests, parallel execution, and flexible XML-based configurations.



Distinctive Features of TestNG:

  • @Test(groups = {"smoke"}) — test grouping
  • @DataProvider — passing data to tests
  • @Test(dependsOnMethods = {"methodA"}) — dependencies between tests


Example with DataProvider:

import org.testng.annotations.DataProvider;import org.testng.annotations.Test;import static org.testng.Assert.assertEquals;

public class DataDrivenTest {

@DataProvider(name = "testData") public Object[][] createData() { return new Object[][] { { 1, 2, 3 }, { 5, -3, 2 }, { 0, 0, 0 } }; }

@Test(dataProvider = "testData") public void testAdd(int a, int b, int expected) { Calculator calc = new Calculator(); assertEquals(calc.add(a, b), expected); }}


3. Mockito — Creating Stubs (Mocks)



Mockito is a library for creating mock objects in Java. It allows you to isolate the component under test from its dependencies (databases, external services) and verify interactions between objects.



Key Mockito Features:

  • mock() — creating a mock object
  • when().thenReturn() — defining behavior
  • verify() — verifying method calls
  • @Mock and @InjectMocks — automatic mock injection


Example of using Mockito:

import static org.mockito.Mockito.*;import org.junit.jupiter.api.Test;

public class UserServiceTest {

@Test void testGetUser() { // Create a mock for the repository UserRepository mockRepo = mock(UserRepository.class); when(mockRepo.findById(1L)).thenReturn(new User("Alice"));

UserService service = new UserService(mockRepo); User user = service.getUser(1L);

assertEquals("Alice", user.getName()); verify(mock

Recommendations