@SpringBootTest
-Starts the full application context. It injects all beans (repositories, services, controllers etc)
- Sets up a transactional rollback, Auto configures a H2 test database
- Provides utilities like:
- @MockitoBean - for replacing real beans
- @DataJpaTest - for fast JPA-only integration tests
- @WebMvcTest - for controller-level tests
@Test
Marks a method as a test case, the JUnit runtime will scan the classes, find methods with the @Test annotation and execute them as part of the test suite
@DisplayName
Provides a readable name for the test
@DataJpaTest
Only loads the Jpa related parts of the spring context, by default it uses H2, in memory database
@DataJpaTest
class RepoTest{}What is the purpose of @WebMvcTest?
It tests only the web layer (controllers and MVC components) without loading the full context, typically using MockMvc to simulate HTTP requests
@Mock
used to create a mock instance of a class or
interface
@InjectMocks
annotation is used to create an instance of
the class you want to test and automatically inject the
fields marked with @Mock
@ExtendWith(MockitoExtension.class)
Mockito annotations need to be enabled in Junit 5 using
this class annotation