Method equivalent for @InjectMocks

There is no public API in Mockito for mock injection. Plus as this annotation is mostly driven on the way things are laid out in a test, it is fairly related to the initialization phase of the test.

Though it might change at some point in the future.

However Mockito annotated fields can be initialized either by the MockitoJUnitRunner or by MockitoAnnotations.initMocks(). They both create mock instances and perform injection.

Also I see in your code that you are using @Autowired – hence spring stuff, probably configured via XML. @InjectMocks wasn’t really developed to work with other dependency injection frameworks, as the development was driven by unit test use cases, not integration tests.

You might want to take a look at springockito, which is another project that tries to ease Mockito mock creation in Spring.

Hope that helps

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)