How do I declare a default parser for Rest Assured 3.0.3 (using Java and TestNG)?
Good day,
I'm admittedly a beginner Java programmer, but I try to research the docs and FAQs to try and overcome the problems. However, this is a problem that I have not been able to overcome. I am using RestAssured (version 3.0.3 as Maven pulled) and cannot get RestAssured to parse "text / plain" content (rather, I cannot get Java to compile the code to do this).
It 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));
}
}
Mistake:
java.lang.IllegalStateException: The expected response body should be validated as JSON, HTML or XML, but text / plain is not supported out of the box. Try registering your own 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 issues: RestAssured cannot be resolved Parser cannot be resolved by variable
I get similar complaints when I try to use the following expression:
RestAssured.defaultParser = Parser.JSON;
For what it's worth, I'm working on Windows 7, 64-bit machine. Using Eclipse Neon.3 (4.6.3) and my JDK is 1.8_131
I have consulted the RestAssured usage and documentation pages, believe I imported the packages correctly, etc. Am I making a rookie mistake somewhere?
source to share
It was a rookie mistake!
Besides statically importing class methods, the compiler also required import of the following classes:
import io.restassured.RestAssured;
import io.restassured.parsing.Parser;
After these declarations, I was able to register the default Parser in the filter_Asset test:
RestAssured.registerParser ("text / plain", Parser.JSON);
source to share