Unit Testing with JUnit and Mockito
Test components in isolation.
Why Unit Tests
A unit test verifies a single class in isolation, with its collaborators replaced by fakes.
- Fast — no Spring context, no database
- Focused — tests one piece of logic
- Reliable — no external dependencies
JUnit 5 Basics
Spring Boot uses JUnit 5 (Jupiter). A test is a method annotated with @Test.
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class CalculatorTest {
@Test
void addsTwoNumbers() {
assertEquals(5, 2 + 3);
}
}All lessons in this course
- Unit Testing with JUnit and Mockito
- Web Layer Tests with MockMvc
- Integration Tests with @SpringBootTest
- Real Dependencies with Testcontainers