ApplicationEventPublisher mocked with MockBean is not injected in component under test

See original GitHub issue

Version of Spring Boot: 1.4.0.M3

I have an integration test, annotated with

@RunWith(SpringRunner::class)
@SpringBootTest
@AutoConfigureMockMvc

and where I have

    @MockBean
    private lateinit var mockApplicationEventPublisher: ApplicationEventPublisher

    @Autowired
    private lateinit var mockMvc: MockMvc

Kotlin code, equivalent to

    @MockBean
    private ApplicationEventPublisher mockApplicationEventPublisher;

    @Autowired
    private MockMvc mockMvc;

The controller under test does call publishEvent() on the event publisher, but when I verify that the calls have been made using

    verify(mockApplicationEventPublisher).publishEvent(ScoreSubmittedEvent()))

I get the following error:

Wanted but not invoked:
org.springframework.context.ApplicationEventPublisher#0 bean.publishEvent(...);
-> at com.ninja_squad.ng2propack.pack.ScoreControllerTest.should submit score(ScoreControllerTest.kt:89)
Actually, there were zero interactions with this mock.

When I debug, I notice that the publisher in the test is indeed a Mockito-generated proxy, but that the publisher injected in the controller under test is the non-mocked instance of org.springframework.web.context.support.GenericWebApplicationContext.

I guess the root caus is that the bean implementing ApplicationEventPublisher is not an actual separate bean, but is the spring application context itself.

A workaround would be to use my own event publisher bean that would simply delegate to the actual spring event publisher, but that’s something I shouldn’t have to do.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

9reactions
renannpradocommented, Jul 29, 2018

I’ve just had the same issue. My workaround was the following

@Bean
@Primary
GenericApplicationContext genericApplicationContext(final GenericApplicationContext gac) {
    return Mockito.spy(gac);
}
5reactions
jnizetcommented, Jun 7, 2016

OK. I’ll open a bug for Spring Framework then, because I think that workarounds shouldn’t be necessary to support that common use-case.

Read more comments on GitHub >

github_iconTop Results From Across the Web

MockBean in a Spring boot test not getting autowired into the ...
In the test, the field is the mocked object that fails "verify()" test because it was never autowired. Why does this happen?
Read more >
Spring Boot Unit Testing | Code With Arho
Learn how to write unit tests for Spring Boot applications. ... we might ask: why not just annotate the repositories with @MockBean then?...
Read more >
Record Spring Events When Testing Spring Boot Applications
Capture and verify Spring Events (ApplicationEvent) when testing your Spring Boot application using @RecordApplicationEvents.
Read more >
Spring Boot Reference Documentation
@MockBean cannot be used to mock the behavior of a bean that is exercised during application context refresh. By the time the test...
Read more >
Mockito.mock() vs @Mock vs @MockBean - Baeldung
Learn the differences between different types of Mocking with Mockito. ... 3rd party library you have no intimate knowledge of is, to say...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found