enableLoggingOfRequestAndResponseIfValidationFails Vs log().all()

545 views
Skip to first unread message

Cema N

unread,
Oct 21, 2016, 4:49:06 AM10/21/16
to REST assured
Hi Johan,


Can you kindly explain what would be the difference between RestAssured.enableLoggingOfRequestAndResponseIfValidationFails(); and
when().get("/test/get").then().log().all();

Even if the end point is not valid, I don't see any log being printed while using
enableLoggingOfRequestAndResponseIfValidationFails
and log is printed always when I use log().all().

I would like to print the log only when there is a failure. What can be used for that?
I'm currently using Rest-Assured 2.9.0


Thanks for your help,
Cema

Johan Haleby

unread,
Oct 21, 2016, 4:55:48 AM10/21/16
to rest-a...@googlegroups.com
When you use "enableLoggingOfRequestAndResponseIfValidationFails" the logs are only printed if you have expectations that are not fulfilled. 

For example when().get("/test/get").then().statusCode(201). If statusCode is 200 then the logs will be logged. If everything is ok then nothing will be logged.

--
You received this message because you are subscribed to the Google Groups "REST assured" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Cema N

unread,
Oct 24, 2016, 12:56:23 AM10/24/16
to REST assured
Hi Johan,

I have tried the following, as error message is displayed with I give status code 201. An error is displayed on the console. Is that what i need to be looking at, as log message as well?

My code is:
RestAssured.enableLoggingOfRequestAndResponseIfValidationFails();
RestAssured.given().get("/test/GET").then().statusCode(201);

Error on console:

Exception in thread "main" java.lang.AssertionError: 1 expectation failed.
Expected status code <201> doesn't match actual status code <200>.

    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:83)
    at org.codehaus.groovy.reflection.CachedConstructor.doConstructorInvoke(CachedConstructor.java:77)
    at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrap.callConstructor(ConstructorSite.java:84)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:60)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:235)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:247)
    at com.jayway.restassured.internal.ResponseSpecificationImpl$HamcrestAssertionClosure.validate(ResponseSpecificationImpl.groovy:598)
    at com.jayway.restassured.internal.ResponseSpecificationImpl$HamcrestAssertionClosure$validate$1.call(Unknown Source)
    at com.jayway.restassured.internal.ResponseSpecificationImpl.validateResponseIfRequired(ResponseSpecificationImpl.groovy:760)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSiteNoUnwrapNoCoerce.invoke(PogoMetaMethodSite.java:210)
    at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:59)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:154)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:166)
    at com.jayway.restassured.internal.ResponseSpecificationImpl.statusCode(ResponseSpecificationImpl.groovy:120)
    at com.jayway.restassured.specification.ResponseSpecification$statusCode$3.callCurrent(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:154)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:166)
    at com.jayway.restassured.internal.ResponseSpecificationImpl.statusCode(ResponseSpecificationImpl.groovy:128)
    at com.jayway.restassured.internal.ValidatableResponseOptionsImpl.statusCode(ValidatableResponseOptionsImpl.java:117)
    at TestRestAssured.RestAssured_SRS_25To29.main(RestAssured_SRS_25To29.java:58)

Johan Haleby

unread,
Oct 24, 2016, 1:33:15 AM10/24/16
to rest-a...@googlegroups.com
You should be able to see what the Rest Assured request specification and response specification (this is structured and is very similar to the actual http response) looks like.

--

Cema N

unread,
Oct 26, 2016, 6:40:02 AM10/26/16
to REST assured
Hi Johan,

I still don't see the logs. I am using request specification and response specification. The error is displayed as below. what am i missing?

Thanks,
Cema
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured...@googlegroups.com.

Johan Haleby

unread,
Oct 28, 2016, 12:18:01 AM10/28/16
to rest-a...@googlegroups.com
Try to do RestAssured.given().log().all().get("/test/GET").then().log().all().statusCode(201);

To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages