Hi!
I'm Unable to mock fetching data from GitHubApi To fetch data i use RestTemplate Exchange
Instead of taking data from github i want to mock my own
Expected :1
Actual :11
Fetch Logic:
@Component
@Setter
public class GitHubApi {
public List<GitHubRepository> getUserRepos(String user) {
restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
addHeaders(headers);
entity = new HttpEntity<>(headers);
ResponseEntity<List<GitHubRepository>> response = restTemplate.exchange(
GITHUB_API_URL + "/users/" + user + "/repos",
HttpMethod.GET, entity,
new ParameterizedTypeReference<List<GitHubRepository>>() {
});
return response.getBody();
}
}
Test Logic:
@Mock
private RestTemplate restTemplate;
@InjectMocks
private GitHubApi gitHubApi;
@Test
public void getUserRepos_ShouldReturnRepositories() {
String username = "user";
GitHubRepository mockedRepository = GitHubRepository.builder()
.id(1L)
.name("repo1")
.fullName("user/repo1")
.build();
List<GitHubRepository> expected = List.of(mockedRepository);
ResponseEntity<List<GitHubRepository>> mockResponse = new ResponseEntity<>(expected, HttpStatus.OK);
HttpHeaders headers = new HttpHeaders();
addHeaders(headers);
when(restTemplate.exchange(
eq(GITHUB_API_URL + "/users/" + username + "/repos"),
eq(HttpMethod.GET),
any(HttpEntity.class),
eq(new ParameterizedTypeReference<List<GitHubRepository>>() {})
)).thenReturn(mockResponse);
List<GitHubRepository> repositories = gitHubApi.getUserRepos(username);
assertNotNull(repositories);
assertEquals(1, repositories.size());
assertEquals("repo1", repositories.getFirst().getName());
}
}
I tries removing eq and any, tried adding HttpEntity instead of any, same with ParameterizedTypeReference
RestTemplateinside your method ("annotating with @Mock does not help"). The@Mockannotated field of your test is independent from the instance in your method. Even the answer that you have accepted provides the same solution as in the linked duplicate ("Constructor Dependency Injection").