Your specific issue might be solved just by removing this assignment:
authService = Mockito.mock(AuthService.class);
Your goal here is to use SpringJUnit4ClassRunner to create some of the dependencies in your graph, and use Mockito to mock other dependencies. However, as in this SO answer, anything marked with @Autowired in your test is created before the @Before method is run. This means that your call to replace authService above will only overwrite the field in your test; it won't adjust or change any of the other @Autowired fields, which have already been created, and which are already using the authService that you've bound in your Spring configuration.
Unit tests
If this is a unit test, and you're simply testing TagsService and TagsRepository, you might want to create those objects manually by calling their constructor with a combination of @Mock and @Autowired fields:
TagRepository repository = new TagRepository(/*mock*/ authService); TagService tagService = new TagService(/*mock*/ authService, /*real*/ repository);
(Mockito provides a way to automate the creation of mocks using @InjectMocks, but because it fails silently if it can't detect an appropriate mock, you might want to avoid that and call a constructor instead. See this article for some good reasons and solutions.)
Note that the above is only when you are working directly with objects in unit tests, not when you're relying on Spring's autowiring to inject objects deeper into your graph.
Integration tests
The test you posted looks like a web or integration test, so it looks like you want to replace the instances of authService with your mock during their creation. Typically this means making a change to the configuration XML or your ApplicationRunner, in order to instruct Spring to use a Mockito mock instead of your real AuthService. Though there are lots of ways to do this, you might want to favor using a Mockito-centric FactoryBean as in this article.
But wait! You may be using an ApplicationRunner that is already customized for Mockito. The line authService = Mockito.mock(AuthService.class); looks very much out-of-place for three reasons:
- you are overwriting an
@Autowired field, which rarely makes sense - you call
Mockito.mock manually despite the existence of MockitoAnnotations.initMocks(this), which is generally preferred - you immediately
reset the mock you create
This suggests to me that your @Autowired authService may already be a mock, and may already have stubbing, which is why you have a leftover reset. If so, then your problem isn't anything about Mockito, other than that you (or someone) have overwritten the field and stubbed your local copy instead of the one that Spring installs throughout your web test. That'd make it an easy fix: Delete your field reassignment and you may be good to go.
checkAuthorityare bothpublicand non-final? That's a common source of mocking trouble.