Skip to Content
0
Former Member
Feb 01, 2018 at 04:22 PM

Replace one dependency with mock in integration test

113 Views

I'm testing a class in an integration test which sends Events using the EventService bean. Since events are async and (seem) super hard to test I want to mock out the EventService dependency in my class. All other dependencies should be resolved as is.

I know that I can simply inject my class with the @Resource annotation und then use setter injection, however:

  • Mocks are not removed from the injected beans if you use setter injection. This causes other integration tests to fail if they reuse the same bean and expect a working version. I've resorted to injecting the original bean in the tearDown method to fix this.

  • I'd like to use constructor injection without defining explicit setters

Since Hybris customizes the test runner pretty heavily I'm at a loss how to integrate common Spring solutions like http://www.baeldung.com/injecting-mocks-in-spring