I am trying to implement tests for an RESTful API using rest-assured, but I am running into a null pointer exception when attempting to invoke a get action. The authorization is a custom scheme, so once I get the authorization signature for the request, I append it as a header to the request:
String auth = ...CUSTOM ALGORITHM ...;
String pragma = ... OTHER CUSTOM HEADER ...;
RequestSpecification requestSpec = new RequestSpecBuilder()
.addHeader("Authorization", auth)
.addHeader("pragma", pragma)
.build();
RestAssured.baseURI = "https://blahblah.staging.somewhere.net";
RestAssured.port = 443;
RestAssured.basePath = "/endpoint_name/somefolder/resource?status=active";
RestAssured.urlEncodingEnabled = false;
requestSpec.get();
This results in the following error:
java.lang.NullPointerException: Cannot get property 'assertionClosure' on null object
Try using RestAssured.given() to call your GET. You can use your requestSpec by doing something like this:
RestAssured.given()
.spec(requestSpec)
.log().all()
.get()
.then()
.log().all()
.statusCode(200);
I encountered the same problem with RestAssured 3.0.7
It looks like that if you build RequestSpecification
with RequestSpecBuilder
some internal state of RequestSpecificationImpl
is not set (responseSpecification
field) what results with NPE when calling get/post methods.
Instead of using:
RequestSpecification requestSpec = new RequestSpecBuilder()
.addHeader("Authorization", auth)
.addHeader("pragma", pragma)
.build();
use:
RestAssured.with().header("Authorization", auth).header("pragma", pragma)
with()
is equivalent of given
which constructs correct RequestSpecification
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