From what I can glean from the web, this should be possible.
I have a Spring @Service I want to test and I want to inject a mocked member into this service. Here's the code snippet:
@Service
public class FooService {
...
public void doSomethingWithBar() {
this.bar.doSomething();
}
@Autowired
private Bar bar;
}
And here's my test class snippet:
@ContextConfiguration(locations = "/test-application-config.xml")
@Listeners(MockitoTestNGListener.class)
public class FooServiceTest extends AbstractTestNGSpringContextTests {
...
@Mock
private Bar bar;
@InjectMocks
@Autowired
private FooService fooService;
}
What's happening in the test is that the actual implementation of Bar.doSomething() is being executed instead of a mocked method. What am I doing wrong here? Is the @Autowired messing things up?
Thanks,
--adam