Forum Discussion

AdamDavisNant's avatar
AdamDavisNant
Occasional Contributor
8 years ago

Get TimeTaken from a REST POST request

I'm trying to write the response times for SOAP and REST calls from the my test cases to a database. I have it working with one exception. The REST POST requests are giving me the following: ERROR:java.lang.NullPointerException: Cannot invoke method getTimeTaken() on null object

 

I'm using the TestListener.AfterRun event. Here is the relevant code:

 

for (myResult in testRunner.getResults())
{
    if((myResult.testStep.config.type == "request") || (myResult.testStep.config.type == "restrequest")){
        //if this is a request we want to get the amount of time it took
        RequestDurationList.add(myResult.response.timeTaken)
        //if the repsonse for the request takes longer than 2 seconds (2000 milliseconds)
        //Mark it as an SLA Failure
        if(myResult.response.timeTaken > 2000){
            SLAFailureValue = "True"
        }
    }
}   

 

My failure is on the BOLD and Italics line.

 

Why is this working for GET requests buy not POSTs?

(ALSO as a note I use this exact code on the SOAP side and I haven't seen any issues there)

 

7 Replies

  • Radford's avatar
    Radford
    Super Contributor

    This sounds a bit odd, I tried a simple rest request test step, with a GET request, and then added a Groovy test step after it with the following code:

     

    testRunner.getResults().each() { result ->
    	assert result.hasResponse()
    	log.info(result.getResponse().getTimeTaken())
    }

     

     

    Now here's the odd thing when my REST request test step has no assertions, all was OK, but once I added an assertion (a simple "Valid HTTP Status Code" one) I then see the behaviour you describe, albeit via hasResponse() returning false (even having disabled assertions seemed to cause the issue).

     

    This doesn't sound right I think I would raise a support call with Smartbear, at this link:

     

    https://support.smartbear.com/product-list/

     

    Please post back with any details Smartbear provide. Note: I am using Ready API 1.7 I had a quick look at the release notes to see if a later version had a fix, but couldn't see anything similar.

     

    As a side note, when I added a Script assertion to the REST request with:

     

    assert messageExchange.hasResponse()
    log.info(messageExchange.getResponse().getTimeTaken())

     

    It seamed to work correctly.

    • AdamDavisNant's avatar
      AdamDavisNant
      Occasional Contributor

      Redford.....Nice catch!! I removed (deleted, disabling doesn't work) the assertion on the REST call and my script worked.

       

      I'll send this over to support and come back when I hear back from them.

      • AdamDavisNant's avatar
        AdamDavisNant
        Occasional Contributor

        Just wanted to follow up since it's been about a week. I opened Case #0023601. Someone replied back that they would reproduce and get back to me, but I'm still waiting to hear back.