I have a test that is using some Spring contexts. In these contexts, a number of beans are declared. I want the test to use the actual implementation of the beans of the contexts, EXCEPT for one of them, for which I want to use a MOCK.
I tried to make the Test a Configuration component (with @Configuration annotation), but the XML seems to take priority over the @Bean annotation, so it doesn't work, this way:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"context1.xml", "context2.xml", ...})
@Configuration
public class MyTest{
@Inject
private MyTargetBean target;
private AnotherBean myMock = mock(AnotherBean.class);
@Bean
public AnotherBean myMock() { return myMock; }
.....
I know that i can define the Mocks in XML, but for that I would need an extra XML file for each test in which I wish to do this. I want to avoid this complexity.
Is there a way to inject a bean (like a mock) in a context apart than through XML?
Thank you!