admin管理员组文章数量:1295721
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
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
Share asked Feb 12 at 3:42 pmalyspmalys 175 bronze badges 3 |1 Answer
Reset to default 1In GitHubApi you create new instance of restTemplate
inside method, so Mockito not injecting restTemplate to class.
You should make restTemplate
as dependency:
@Component
@Setter
public class GitHubApi {
private final restTemplate; // <-- Here rest template is declared as dependency of this class.
// constructor or use @RequiredArgsConstructor if you use lombok.
public GitHubApi(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public List<GitHubRepository> getUserRepos(String user) {
// do not create restTemplate here, because it will rewrite mock with another instance.
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();
}
}
本文标签: javaSpring Mockito Unable to mock RestTemplateStack Overflow
版权声明:本文标题:java - Spring Mockito Unable to mock RestTemplate - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741622440a2388888.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
RestTemplate
inside your method ("annotating with @Mock does not help"). The@Mock
annotated 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"). – knittl Commented Feb 12 at 13:37