title: Mockito: How to partially use @InjectMocks-Annotation for MockMvc testing?
body: <p>I need to test an controller and for the most parts it is still enough to use <code>MockMvc</code>. But there are still some branches which are very hard to generate an test case for. So I use <code>Mockito</code> for stubbing methods and to throw exception for this special test cases.</p>
  
  <p>My controller implements an service and the setup looks like the following:</p>
  
  <p><strong>MyController.class</strong>:</p>
  
  <pre><code>@RestController
  @RequestMapping("/myTest")
  public class MyController {
  
      @Autowired
      private MyService myService;
  
      @RequestMapping(value = "/", method = { RequestMethod.POST }, produces = "application/json")
      public final int controllerMethod() {
          return myService.serviceMethod();
      }
  }
  </code></pre>
  
  <p><strong>MyService.class</strong>:</p>
  
  <pre><code>@Service
  public class MyService {
      @PreAuthorize("hasRole('ROLE_ADMIN')")
      public int serviceMethod() {
          return 5;
      }
  }
  </code></pre>
  
  <p>So I have multiple test methods and in some methods I mock the <code>MyService.class</code> to throw an exception for example.
  But in other methods I want to test the normal functionalities. So here occures the problem.
  If the tests are not running in the right order, the normal functionality test won't work, because <code>MyService.class</code> is still mocked, I guess.
  So in this example, the serviceMethod returns null instead of 5.</p>
  
  <p>Is there a way to partially InjectMocks - so I mean only for special methods and not for the class?</p>
  
  <p>Here is the example code:</p>
  
  <p><strong>MyControllerTest.class</strong>:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(classes = Config.class)
  @WebAppConfiguration
  public class MyControllerTest {
      @Autowired
      private WebApplicationContext webApplicationContext;
  
      @Autowired
      MyService myService;
  
      @InjectMocks
      @Resource
      MyController myController;
  
      @Resource
      private FilterChainProxy springSecurityFilterChain;
  
      private MockMvc mockMvc;
  
      @Before
      public void setup() throws SQLException {
          this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
      }
  
      @Test
      @WithMockUser(username = "validUser", roles = { "ADMIN" })
      public void testMyController() throws Exception {
          mockMvc.perform(post("/myTest/").with(csrf())).andDo(print()).andExpect(status().is2xxSuccessful()).andExpect(jsonPath("$", is(5)));
      }
  
      @Test
      @WithMockUser(username = "validUser", roles = { "ADMIN" })
      public void testMyControllerException() throws Exception {
          myService = Mockito.spy(MyService.class);
  
          MockitoAnnotations.initMocks(this);
  
          Mockito.doThrow(new IndexOutOfBoundsException()).when(myService).serviceMethod();
  
          mockMvc.perform(post("/myTest/").with(csrf())).andDo(print()).andExpect(status().is4xxClientError());
  
          Mockito.verify(myService, Mockito.times(1)).serviceMethod();
      }
  }
  </code></pre>
  
  <p>So if I run especially only one of these tests, both working well. 
  But if I run both together and <code>testMyControllerException()</code> will be executed first, the <code>testMyController()</code> fails because of the mocked myService-Instance, I guess.</p>
  
  <p><strong>Is there any way to fix this problem or must I split this tests into two different testclasses?</strong></p>
  
  <p>I tried to find a way for unmocking the <code>myController.class</code> over the <code>@After</code>-Annotation with <code>Mockito.reset(myController);</code>. But did't worked for me.</p>