Not using any Spring Boot test annotation means not using @SpringBootTest
or any other Spring Boot Test slice annotation. There are many Spring Boot test annotations that auto-configure the Spring TestContext for you. I can't find a single page that shows them all (there are potentially more added), but the Spring Boot documentation mentions them, starting from 8.3.12+.
You can easily identify a Spring Boot test annotation that you put on top of your class by taking a look at the import statement. They all start with org.springframework.boot.test
, e.g. @WebMvcTest
, @DataJpaTest
, @WebfluxTest
, etc.
Suppose you're not relying on a Spring Boot test annotation and configure your Spring test context manually. In that case, you'll need to add @TestExecutionListeners({ MockitoTestExecutionListener.class, ResetMocksTestExecutionListener.class })
on top of your test class to use @MockBean/@SpyBean
.
@Mock
vs. @MockBean
is a different pitfall. With @MockBean/@SpyBean
you replace/add beans inside your Spring TestContext, while @Mock
is for plain unit testing without any Spring TestContext.