title: Impossible to write integration test controller with DAO mock?
body: <p>I become crazy, I tried all possible combination of various test Runners and possibles annotation for testing, the nearest solution of my need is following :</p>
  
  <pre><code>@RunWith(SpringRunner.class)
  @SpringBootTest(classes = {MyApplication.class})
  @WebAppConfiguration
  public class MyControllerTest {
  
      MockMvc mockMvc;
  
      // My DAO is an interface extending JpaRepository
      @Mock
      MyDAO myDAO;
  
      @Autowired
      WebApplicationContext webApplicationContext;
  
      @Before
      public void setUp() throws Exception {
          List&lt;MyItem&gt; myItems = new ArrayList(){{
              // Items init ...
          }}
          Mockito.when(myDAO.findAll()).thenReturn(myItems);
          /* Other solution I tried with different annotations: 
          * given(myDAO.findAll()).willReturn(myItems);
          * this.mockMvc = MockMvcBuilders.standaloneSetup(myController).build();
          */
          this.mockMvc = webAppContextSetup(webApplicationContext).build();
  
      }
  
      @After
      public void tearDown() throws Exception {
  //        Mockito.reset(myDAO);
      }
  
      @Test
      public void getItems() {
          String res = mockMvc.perform(get("/items"))/*.andExpect(status().isOk())*/.andReturn().getResponse().getContentAsString();
          assertThat(res, is("TODO : string representation of myItems ..."));
          assertNull(res); // For checking change in test functionning
      }
  }
  </code></pre>
  
  <p>I well enter in debug mode in my controller method, in service method but when I see DAO type, it is not a Mock and findAll() always return empty ArrayList(), even when I do : </p>
  
  <pre><code>Mockito.when(myDAO.findAll()).thenReturn(myItems);
  </code></pre>
  
  <p>I do not have exception raised, my DAO is not mocked and I don't know how to do despite all tuto I found.
  The nearest tuto of my need I found was this <a href="https://www.luckyryan.com/2013/08/24/unit-test-controllers-spring-mvc-test/" rel="nofollow noreferrer">Unit Test Controllers with Spring MVC Test</a> but not enought because he wants mock service injected into controller in order to test controller, I wand to mock DAO injected into real Service injected into Controller (I want to test Controller + Service).</p>
  
  <p>It seems to me that I already did that by using an annotation on the test class which specified what class had to be instanciate by spring application in test mode and what class had to be mocked but I don't remember '-_-.</p>
  
  <p>Need your help, its making me crasy !</p>
  
  <p>Thank you very much !!!</p>