0

Spring Boot test documentation says:

If your test uses one of Spring Boot’s test annotations (such as @SpringBootTest), this feature is automatically enabled. To use this feature with a different arrangement, listeners must be explicitly added...

What does "To use this feature with a different arrangement, listeners must be explicitly added" mean?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
LiLi
  • 391
  • 3
  • 11
  • 2
    Could you clarify what specifically you do and don't understand? _"a different arrangement"_ just means if you're **not** using the Spring Boot test annotations. – jonrsharpe Dec 22 '21 at 19:41
  • I don`t know when to use these listeners. what means not using the Spring Boot test annotations. Is that measn using Mockito @Mock instead of SpringBoot @MockBean? – LiLi Dec 23 '21 at 04:26

1 Answers1

0

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.

rieckpil
  • 10,470
  • 3
  • 32
  • 56