Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Null-pointer exception when invoking get in rest-assured: Cannot get property 'assertionClosure' on null object

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

like image 756
Selena Avatar asked May 13 '14 15:05

Selena


2 Answers

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);
like image 63
Bert Avatar answered Nov 14 '22 23:11

Bert


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

like image 30
Jan Gurda Avatar answered Nov 14 '22 22:11

Jan Gurda