Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I declare a default parser for Rest Assured 3.0.3 (using Java and TestNG)?

Good day folks,

I am an admittedly novice Java programmer but I take care to research docs and FAQ's to try to get past issues. This is a problem that I have not been able to overcome, however. I am using RestAssured (version 3.0.3, as pulled by Maven) and cannot get RestAssured to parse "text/plain" content (rather, I cannot get Java to compile the code to do so).

This compiles but gives an error:

import static io.restassured.RestAssured.*;
import static io.restassured.matcher.RestAssuredMatchers.*;
import static org.hamcrest.Matchers.*;

import static io.restassured.module.jsv.JsonSchemaValidator.*;

import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.testng.annotations.AfterClass;

public class TestNG2 {

/*
   userName, passWord and server defined here as protected static Strings
*/
    @Test
    public void filter_Asset(){

        given().
            auth().basic(userName, passWord).
        when().
            get ("http://" + server +"/api/filter?type=$tAsset").
        then().
            statusCode(200).
            body("count", greaterThan(0));
    }
}

The error:

java.lang.IllegalStateException: Expected response body to be verified as JSON, HTML or XML but content-type 'text/plain' is not supported out of the box. Try registering a custom parser using: RestAssured.registerParser("text/plain", );

However, when I try to include the following line in the filter_Asset test:

RestAssured.registerParser("text/plain", Parser.JSON);

The code will not compile with the following complaint:

java.lang.Error: Unresolved compilation problems: RestAssured cannot be resolved Parser cannot be resolved to a variable

I receive similar complaints when I try to use the following declaration:

RestAssured.defaultParser = Parser.JSON;

For what it's worth, I am working on a Windows 7, 64-bit machine. Using Eclipse Neon.3 (4.6.3) and my JDK is 1.8_131

I've consulted the RestAssured usage and documentation pages, believe am importing the packages correctly, etc. Am I making a rookie error somewhere?

like image 743
m1gp0z Avatar asked Jun 12 '17 15:06

m1gp0z


People also ask

What is Hamcrest matchers in Rest assured?

Hamcrest Matchers are optional, and are not strictly needed for REST Assured. They allow us to write more expressive unit tests. Gson is automatically used by REST Assured for JSON (de)serialization, as we will see in the examples. Rest Assured can also work with Jackson, if that is available in the classpath.

What are the different method and classes present in Java rest assured?

REST Assured is a Java library for testing RESTful APIs. It is widely used to test JSON and XML-based web applications. In addition, it fully supports all REST methods like the GET, PUT, POST, PATCH, and DELETE.

How do you test plain text response body with rest assured?

For this, we need to configure Rest Assured such that it can grasp a plain/text type Response. We need to use the registerParser method which is a part of the RestAssured class. Then pass text/plain and Parser. Text as parameters to the registerParser method.


1 Answers

It was a rookie mistake!

In addition to statically importing the class methods, the compiler also required importing of the following classes:

import io.restassured.RestAssured;
import io.restassured.parsing.Parser;

After those declarations, I was able to register the default Parser in the filter_Asset test:

RestAssured.registerParser("text/plain", Parser.JSON);

like image 190
m1gp0z Avatar answered Nov 14 '22 03:11

m1gp0z