Re: [rest-assured] NullPointerException when trying to set default responseSpecification to log everything

122 views
Skip to first unread message

Johan Haleby

unread,
Dec 5, 2012, 1:17:22 AM12/5/12
to rest-a...@googlegroups.com
Hi, 

Currently there's no way to enable logging for all requests and/or responses but this is something that we should support so good point. Could you please add it as an issue on the issue tracker at google code?

The NPE that you experience is a bug though and it ought to be addressed as well (please mention this in the bug report as well).

Regards,
/Johan

On Tue, Dec 4, 2012 at 9:12 PM, Matt Brown <octa...@gmail.com> wrote:
In a @Before method, I have something like

    RestAssured.responseSpecification = new ResponseSpecBuilder().build().log().all(true);

This results in a NullPointerException with a stacktrace like:

java.lang.NullPointerException: Cannot get property 'restAssuredConfig' on null object
at org.codehaus.groovy.runtime.NullObject.getProperty(NullObject.java:56)
at org.codehaus.groovy.runtime.InvokerHelper.getProperty(InvokerHelper.java:156)
at org.codehaus.groovy.runtime.callsite.NullCallSite.getProperty(NullCallSite.java:44)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:227)
at com.jayway.restassured.internal.LogSpecificationImpl.getLogConfig(LogSpecificationImpl.groovy:45)
at com.jayway.restassured.internal.LogSpecificationImpl.this$2$getLogConfig(LogSpecificationImpl.groovy)
at com.jayway.restassured.internal.LogSpecificationImpl$this$2$getLogConfig.callCurrent(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:46)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:133)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:141)
at com.jayway.restassured.internal.LogSpecificationImpl.getPrintStream(LogSpecificationImpl.groovy:29)
at com.jayway.restassured.internal.ResponseLogSpecificationImpl.super$2$getPrintStream(ResponseLogSpecificationImpl.groovy)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1054)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodOnSuperN(ScriptBytecodeAdapter.java:128)
at com.jayway.restassured.internal.ResponseLogSpecificationImpl.getPrintStream(ResponseLogSpecificationImpl.groovy:92)
at com.jayway.restassured.internal.ResponseLogSpecificationImpl.logWith(ResponseLogSpecificationImpl.groovy:83)
at com.jayway.restassured.internal.ResponseLogSpecificationImpl.this$3$logWith(ResponseLogSpecificationImpl.groovy)
at com.jayway.restassured.internal.ResponseLogSpecificationImpl$this$3$logWith.callCurrent(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:46)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:133)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:145)
at com.jayway.restassured.internal.ResponseLogSpecificationImpl.all(ResponseLogSpecificationImpl.groovy:43)
at com.jayway.restassured.internal.ResponseLogSpecificationImpl.all(ResponseLogSpecificationImpl.groovy)

Looking at the code in the trace, it seems like LogSpecificationImpl.getLogConfig is trying to examine requestSpecification.restAssuredConfig, which fails because there is no requestSpecification set with the (default) response specification.

Is there a standard way to set logging for all responses in a single place?

Matt Brown

unread,
Dec 5, 2012, 3:58:21 PM12/5/12
to rest-a...@googlegroups.com

Johan Haleby

unread,
Dec 5, 2012, 4:09:49 PM12/5/12
to rest-a...@googlegroups.com
Actually you can do this (sorry to confuse you). Calling "log().all(true)" is actually just a shortcut for adding the "ResponseLoggingFilter" filter. So what you can do is something liket this:

RestAssured.filters(new ResponseLoggingFilter());

You can also log only if the status code indicates an error:

RestAssured.filters(new ErrorLoggingFilter());

Now you'll log the response for all Rest Assured responsens. 

Unfortunately it doesn't seem like ResponseSpecBuilder have support for adding filters, this is something that we should add support for!

Regards,
/Johan 

Matt Brown

unread,
Dec 5, 2012, 4:27:09 PM12/5/12
to rest-a...@googlegroups.com
Thanks again Johan, I happened to be just looking into how to add a ResponseLoggingFilter in one-place for all responses. Works great!
Reply all
Reply to author
Forward
0 new messages