I have problem with rest-assured. I'm reciving the following error
java.lang.IllegalStateException: Cannot parse object because no supported Content-Type was specified in response. Content-Type was 'null'.
My rest-controller look like below
@Slf4j
@RestController
@RequestMapping(ApiUrls.SESSIONS_API)
@RequiredArgsConstructor
public class SessionsApi {
private final SessionsService sessionsService;
@GetMapping(produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public List<SessionDto> sessions(@AuthenticationPrincipal CryptoUser user,
@RequestParam(required = false, defaultValue = "false") boolean includeExpired) {
log.info("User sessions of user [id {} username {}]", user.getUserId(), user.getUsername());
return sessionsService.getSessionsOfUser(user, includeExpired);
}
and the test
SessionDto[] sessionDtos = given().mockMvc(mvc)
.log().all()
.header("Accept","application/json")
.get(ApiUrls.SESSIONS_API)
.then()
.extract()
.as(SessionDto[].class);
is rest assured logs I get
Request method: GET
Request URI: http://localhost:8080/api/sessions?includeExpired=true
Proxy: <none>
Request params: includeExpired=true
Query params: <none>
Form params: <none>
Path params: <none>
Headers: Content-Type=application/json
Accept=application/json
Cookies: <none>
Multiparts: <none>
Body: <none>
I tried to add produces = MediaType.APPLICATION_JSON_VALUE in getmapping but anyway is didn't work. Am I missing something ? Thread is not even getting into controller method
If your content type is null you can try something like this
given().mockMvc(mvc)
.log().all()
.header("Accept","application/json")
.get(ApiUrls.SESSIONS_API)
.then()
.contentType(isEmptyOrNullString())//This is where you handle null
.extract()
.as(SessionDto[].class);
Also this link might be useful: https://github.com/rest-assured/rest-assured/issues/636
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With