For Testing, I am using MockMvc to make a POST request to a Controller and I would like to Mock a particular service call so that it does nothing (with the Mockito.doNothing()). If I call that method within the @Test, then it works perfectly and the method is not called. The problem occurs when I do a mockMvc.perform(..) and the execution hits the controller, the method doNothing() is ignored and the function is executed. I am running the Spring MVC tests in a WebApplicationContext.
This is the setup:
@Mock
private MyService myService;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
}
This part works. When myFuncToIgnore() is called, it is skipped.
@Test
public void myTest() throws Exception {
Mockito.doNothing().when(myService)
.myFuncToIgnore(isA(String.class), isA(String.class));
myService.myFuncToIgnore("", "");
}
This part doesn't work. When the mockMvc calls the controller the block is executed. Please note that the controller calls an interface, whose implementation calls MyService, which is an interface with an impl called MyServiceImpl). Here is the code:
mockMvc.perform(
post(serviceContextPath)
.contentType(MediaType.APPLICATION_JSON)
.header("X-Request-Language", "en"))
.andDo(print())
.andExpect(status().isOk())
.andReturn();
Do you have any suggestions please?