1

I need to mock @Component beans for all my tests.

I ve tried to use a @Primary and @TestConfiguration and excludeFilter in the @ComponentScan but it does not work.

How can I put a mock bean into the ApplicationContext instead of my real bean?

  • You use a @MockBean – Lesiak Feb 11 '21 at 13:31
  • You would use `@Mock` and `@InjectMocks` annotations. This answer has some helpful information on mocking @Component beans and creating junit and integration tests https://stackoverflow.com/a/42170731/15187468 – redwingsdan Feb 11 '21 at 21:03
  • Thank you, but I need to mock a bean in the ApplicationContext and overwrite this bean for all tests. – Rostyslav Terzman Feb 12 '21 at 14:26
  • Did you already try the hint in the comment of @Lesiak ? This does the trick and is IMHO the the answer to your question or do you need some more help with an example? – mle Feb 14 '21 at 17:27

0 Answers0