Maciej Rudnicki
Maciej Rudnicki

Reputation: 141

Response Content-Type was 'null'. Rest assured exception

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

Upvotes: 3

Views: 3097

Answers (2)

Cansın Tartıcı
Cansın Tartıcı

Reputation: 43

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

Upvotes: 1

Nakesh
Nakesh

Reputation: 576

Add produces = MediaType.APPLICATION_JSON_UTF8_VALUE in @RequestMapping instead of @GetMapping.

Replace

 @RequestMapping(ApiUrls.SESSIONS_API)

With

@RequestMapping(value = ApiUrls.SESSIONS_API, produces = {
    MediaType.APPLICATION_JSON_VALUE })

Upvotes: 0

Related Questions