Rest-assured logging response on assertion failure

3,826 views
Skip to first unread message

mohan v

unread,
Apr 11, 2014, 10:20:49 AM4/11/14
to rest-a...@googlegroups.com
Hi,

I would like to have response printed on assertion failure

I use the following code:
ResponseSpecBuilder responseSpecBuilder
Response  response
 responseSpecBuilder
                        .expectBody(property, equalTo(""somevalue))
                        .build()
                        .validate(response);

2.3.1 supports RestAssured.enableLoggingOfRequestAndResponseIfValidationFails();, but does nt help in above case

Any workaround  to have respone printted at least on failure


mohan v

unread,
Apr 11, 2014, 12:20:24 PM4/11/14
to rest-a...@googlegroups.com
I alos get this errror in report

Was using  the following when I got error. Why does it need request specifcation
builder.build().log().ifValidationFails().validate(getScenario().getResponse());

java.lang.IllegalStateException: Cannot configure logging since request specification is not defined. You may be misusing the API.
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
	at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:77)
	at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorSite.java:102)
	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:57)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:182)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:190)
	at com.jayway.restassured.internal.LogSpecificationImpl.getLogConfig(LogSpecificationImpl.groovy:48)
	at com.jayway.restassured.internal.LogSpecificationImpl.this$2$getLogConfig(LogSpecificationImpl.groovy)

Johan Haleby

unread,
Apr 14, 2014, 1:43:54 AM4/14/14
to rest-a...@googlegroups.com
Yes you are not using the API correctly in the last case. There's an issue for improving "enableLoggingOfRequestAndResponseIfValidationFails" when using specifications since specs currently override this behavior.

/Johan




--
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...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

mohan v

unread,
Apr 14, 2014, 9:36:05 AM4/14/14
to rest-a...@googlegroups.com
At the moment, I am trying to manually print request and response when it fails
Response can be printed using response.prettyPrint();

Is there anyway to print request using prettyPrint()  from request specification
I know given().all().log does the job, but I want individual response print  as mentioned above

Johan Haleby

unread,
Apr 14, 2014, 10:00:59 AM4/14/14
to rest-a...@googlegroups.com
Yes you can do log().all() after expect or then. For example:

when().get("/x").then().log().all(). .. // Pretty prints response.

To print the response only if it fails do:

when().get("/x").then().log().ifValidationFails(). .. 

/Johan

mohan v

unread,
Apr 14, 2014, 11:26:04 AM4/14/14
to rest-a...@googlegroups.com

I wanted to pretty print was request  like  I have RequestSpecification and call any method on this obejct  to do equivalent of prettyprint

I wanted to something from cucmber

 @After
    public void afterScenario(Scenario result) {
          if (result.getStatus().equals("failed")) {

               RequestSpecification  requestSpecification =getRequestSpecification();

                log.info(" Reeuqtes failed was : " +requestSpecification.prettyprint() )

Johan Haleby

unread,
Apr 14, 2014, 1:11:20 PM4/14/14
to rest-a...@googlegroups.com
Then you can do:

given().log().ifValidationFails().when().get("/x"). ..

/Johan

mohan v

unread,
Apr 16, 2014, 8:14:38 AM4/16/14
to rest-a...@googlegroups.com
I guess I was not clear early. Let me re-pharse

Currently my framework everywhere uses   given().spec(requestSpec).expect().spec(responseSpec).get(relUrl);

ResponseSpecBuilder responseSpecBuilder= new ResponseSpecBuilder();
ResponseSpecification respSpec = responseSpecBuilder.expectBody(anything()).build()

Request is sent in one Step and Response Validfation is done  in another  Step  

Request is sent using the follolwing

given().spec(requestSpec).expect().spec(responseSpec).get(relUrl);


Respone validation is done by the following in another step
ResponseSpecBuilder builder =new ResponseSpecBuilder();

builder
  .expectBody("email", equalTo("someEmail"))
  .expectBody("title", equalTo("Some Title"));
  
/// Validate Response
builder.build().validate(getScenario().getResponse());  


In above Scenario When Validation is not done on given...  statement  as ResponsSpec expects anything(or response validation is not done)
the statement given.log().ifValidationFails().when().get("/x")  does not log when Validation Fails, It logs irrespective of validation, as Validation is never
done  in  given statement

So, In  a nut shull, when you use given().log().ifValidationFails().spec(requestSpec).expect().spec(responseSpec)
log().ifValidationFails()  is not effective as the logging is always done irrespective of result, and may be  also because  validation is done
in builder.build().validate(getScenario().getResponse()); 

Johan Haleby

unread,
Apr 22, 2014, 1:39:11 AM4/22/14
to rest-a...@googlegroups.com
This is probably related to issue 323

Regards,
/Johan

mohan v

unread,
Apr 22, 2014, 5:16:00 AM4/22/14
to rest-a...@googlegroups.com
Yep. It looks same issue
Any idea, if it is going to be fixed in near future

Johan Haleby

unread,
Apr 22, 2014, 6:30:05 AM4/22/14
to rest-a...@googlegroups.com
Can't give any promises, but I'll look into when I find some time. You can of course also help out if you like by providing a pull request.

Regards,
/Johan

JP

unread,
May 4, 2014, 3:54:28 PM5/4/14
to rest-a...@googlegroups.com
Mohan,

Check out my comments on this issue as it is a workaround for you:


Thanks,
JP
Reply all
Reply to author
Forward
0 new messages