Sobes.tech
Junior — Middle

What tools for unit testing do you use or know?

sobes.tech AI

Answer from AI

In Java, the most commonly used tools for unit testing are:

  • JUnit — the main framework for writing and running tests.
  • Mockito — a library for creating mock objects and simulating the behavior of dependencies.
  • AssertJ or Hamcrest — libraries for convenient and readable assertions.

Example of a simple test with JUnit 5:

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

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

Mockito helps, for example, to mock services or repositories to test logic without real dependencies.