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.
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();
}
}
When I run the test case, it is not using the mock Repository, instead of that using the @Autowired
Repository which is mentioned in the Service class.
Can anyone please explain me how to mock the repository from controller class.
Posting so many questions in this blog, but am not getting any responses.