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?
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.
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.
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.
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);
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