Spring Framework :: Make Spring ignore your mocks
If you are using the core Spring Framework (that is, not Spring Boot), then you may have encountered a problem where Spring doesn’t seem
to completely ignore mocked beans in your tests: Perhaps Spring attempts to inject beans into them or run your
@PostConstruct lifecycle methods.
If you are using Spring Boot and have this behavior. Then there is a big chance that you are holding it wrong.
About the problem
You shouldn’t have this problem with Spring Boot. If you do, then you are not using
@MockBean based mocks.
See the next section for how you should be using mocks with Spring Boot.
When you mock a Spring bean in your non-Spring Boot tests, and if the mock is based on a class
and not an interface, then Spring attempts to autowire any dependencies it may have. Spring
also attempts to invoke any
@PostConstruct initializers that it may have. But … you just
want to mock that d**n bean, right?
ProblemWithoutSpringBootGreeterServiceTests for a demo of the problem.
Solution: Spring Boot based code
With Spring Boot you just have to mock your beans using the
SpringBootPoweredGreeterServiceTests for a very elegant solution.
Solution: Core Spring Framework based code
SolutionWithoutSpringBootGreeterServiceTests for the solution to it: Simply wrap your mock inside a
This ensures that Spring doesn’t initialize it (performing injection and calling
Try it out