title: Mock Repository From Controller class using Spring Boot and JUnit
body: <p>I am writing the test cases for my controller class which is a Spring Boot Application and I want to write the test cases only for controller class which invokes Service and Service to Repository. I am using SpringBootTest which is used for to create the instances for all of my beans. I want to mock only Database calls and external api calls.</p>
  
  <pre><code>MyController {
  
      @Autowired
      MyService service;
  
      //Api call for getDetails
      service.getDetails();
  }
  
  
  MyService {
  
     @Autowired
     MyRepository repo;
  
  }
  
  MyControllertest {
      @Autowired
      MyController controller;
  
      @Mock
      MyRepository repoMock;
  
      @Before
      public void setup(){
          // intit mocks
      }
  
      @Test
      public void myTest(){
          when(repoMock.getDetails()).thenReturn(null);
          controller.getdetails();
      }
  }
  </code></pre>
  
  <p>When I run the test case, it is not using the mock Repository, instead of that using the <code>@Autowired</code> Repository which is mentioned in the Service class.</p>
  
  <p>Can anyone please explain me how to mock the repository from controller class. </p>
  
  <p>Posting so many questions in this blog, but am not getting any responses.</p>