Hier ist mein Controller:Probleme mit spöttischen restTemplate
@RequestMapping(
value = "/{owner}/{repositoryName}",
method = RequestMethod.GET,
produces = "application/json"
)
public RepoInfo repoInfo(@PathVariable String owner, @PathVariable String repositoryName) {
return restTemplate.getForObject(githubApiUrl + owner + "/" + repositoryName, RepoInfo.class);
}
Und hier sind Tests Methoden:
@Before
public void mockGithubApiResponse() {
Mockito.when(restTemplate.getForObject(githubApiUrl + dummyOwner + "/" + dummyRepoName, RepoInfo.class)).thenReturn(dummyRepoInfoObject);
}
@Test
public void shouldReturnRepoInfoObject() throws Exception {
Gson gson = new Gson();
String jsonRepo = gson.toJson(dummyRepoInfoObject);
this.mockMvc.perform(get("http://localhost:" + this.port + "/" + repoControllerMappingPrefix + dummyOwner + "/" + dummyRepoName)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().json(jsonRepo));
}
Aber es scheint nichts zu spotten. Der Controller reagiert immer noch auf externen Service.