Junior — Middle
What tools or approaches do you use when writing tests?
sobes.tech AI
Answer from AI
When writing tests in Java, I usually use the following tools and approaches:
- JUnit — the main framework for unit testing. It allows writing tests, using annotations (@Test, @Before, @After), and checking results with assertions.
- Mockito — for creating mock objects and simulating dependency behavior.
- AssertJ or Hamcrest — for more expressive and readable assertions.
- TestNG — an alternative framework with extended capabilities, such as test parameterization.
Approaches:
- Write independent, isolated tests.
- Use the AAA principle (Arrange, Act, Assert).
- Cover both positive and negative scenarios.
- Write tests that are easy to maintain and read.
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));
}
}