I have a ITest in Spring which looks like this:
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class AuthControllerITest {
@Autowired
private TestRestTemplate template;
@Test
public void shouldGetAccessToken() throws Exception {
String email = "[email protected]";
String url = AuthController.GET_TOKEN_PATH+"?email="+email;
ResponseEntity<AuthResponse> response = template.getForEntity(url, AuthResponse.class);
assertThat(response.getStatusCode().is2xxSuccessful());
}
}
But method execution causes an error
org.springframework.web.client.RestClientException: Error while extracting response for type [class com.my.app.security.ui.response.AuthResponse] and content type [application/json]
AuthResponseController action and Response model look like following:
@GetMapping(AuthController.GET_TOKEN_PATH)
public ResponseEntity<AuthResponse> auth(@RequestParam(required = false) String email)
{
if (null == email) {
throw new RuntimeException("Email must be provided");
}
User user = userRepository.findUserByEmail(email);
return ResponseEntity.ok(new AuthResponse(jwtService.createToken(user)));
}
public class AuthResponse {
private String token = "some token";
public AuthResponse(String token) {
this.token = token;
}
public String getToken() {
return token;
}
}
It works when I switch type from specific class to just a String
:
ResponseEntity<String> response = template.getForEntity(url, String.class);
But I want to get AuthResponse
object – besides the status code I want to check if generated token is correct.
So could you tell me why I cannot get the mapped AuthResponse
object?