How to override the behavior of Spring @Autowired

后端 未结 3 1168
北荒
北荒 2020-12-19 05:06

A little background:

I am Using Spring 2.5, and specifically Spring IOC and annotations.

I am using @Autowired in my code (the Autowiring is don

3条回答
  •  生来不讨喜
    2020-12-19 05:42

    I know this question is quite old but a I think an answer might still be useful for others.

    Since you probably do not want to mix both Foo and MockFoo within your context, I would suggest to remove Foo from the component-scanning. This could be done for example by specifying include/exclude filters on the .

    However if you are implementing unit tests, I would rather suggest not using a Spring context and just implementing "pure" unit tests by injecting mock-ups of the dependencies manually, so that you are only testing a single class. This can be achieved more easily by using a mocking framework like Mockito.

提交回复
热议问题