Forum Discussion

hindza's avatar
hindza
New Contributor
7 years ago
Solved

HTTP Status Codes Assertion Values disappear when re-opening SoapUI

Hi All, 

 

I'm getting some strange behavior from Soap UI. 

 

I'm using the following script to insert a Valid HTTP Status Code Assertion, an Invalid HTTP Status Code Assertion, and a string that I get from the Response. 

This is one block of scripts, and I use 50-250 of the in one Groovy Step to fill out all the Test Step Assertions in my Test Case:

 

//This part adds the Invalid HTTP Status Codes in an Assertion targeting the named Test Step.
//Status codes can be added or removed as it suits your needs.
def InvalidAssertion1 = testRunner.testCase.testSteps["Test Step Name"].addAssertion("Invalid HTTP Status Codes");
java.lang.reflect.Field InvalidStatusAssertionCodesField1 = InvalidAssertion1.getClass().getDeclaredField("codes");
InvalidStatusAssertionCodesField1.setAccessible(true);
InvalidStatusAssertionCodesField1.set(InvalidAssertion1, "400,401,402,403,404,500")

//This part adds the Valid HTTP Status Codes in an Assertion targeting the named Test Step.
//Status codes can be added or removed as it suits your needs.
def ValidAssertion1 = testRunner.testCase.testSteps["Test Step Name"].addAssertion("Valid HTTP Status Codes");
java.lang.reflect.Field ValidStatusAssertionCodesField1 = ValidAssertion1.getClass().getDeclaredField("codes");
ValidStatusAssertionCodesField1.setAccessible(true);
ValidStatusAssertionCodesField1.set(ValidAssertion1, "200")

//This part takes the Request from the targeted Test Step and displays it onscreen as a string.
//Then it adds an Assertion of the "Contains" type and inserts the string (from the previous step) into the targeted Test Step.
def Request1 = testRunner.testCase.getTestStepByName('Test Step Name').testRequest.response.contentAsString
log.info(Request1)
def Assertion1 = testRunner.testCase.testSteps["Test Step Name"].addAssertion("Contains")
Assertion1.setToken(Request1)

When I finish adding the Assertions, I run the Test Case and everything is fine. 

Now, the funny thing is: when I save the project and close SoapUI, then open it again and run the same TC my tests fail because the Valid HTTP Status Code Assertion and the Invalid HTTP Status Code Assertion are empty. No codes are present even though they were there before, and everything was running smoothly. 

 

I've tried if this is replicable on different computers, and it is. 
Did anyone had any similar experiences? 

 

Thanks in advance to the SoapUI community. 

 

  • You're using reflection to get access to fields that you shouldn't normally have access to instead of using the public API. For instance, you might just be altering the object in memory, but the public methods would also change the representation in the project file.

     

    Try setting the codes like this instead:

     

    testRunner.testCase.testSteps["Test Step Name"]
    .addAssertion("Invalid HTTP Status Codes")
    .setCodes("400,401,402,403,404,500")

     Always be looking at the API Docs. I found setCodes from here: https://www.soapui.org/apidocs/com/eviware/soapui/security/assertion/ValidHttpStatusCodesAssertion.html

2 Replies

  • JHunt's avatar
    JHunt
    Community Hero

    You're using reflection to get access to fields that you shouldn't normally have access to instead of using the public API. For instance, you might just be altering the object in memory, but the public methods would also change the representation in the project file.

     

    Try setting the codes like this instead:

     

    testRunner.testCase.testSteps["Test Step Name"]
    .addAssertion("Invalid HTTP Status Codes")
    .setCodes("400,401,402,403,404,500")

     Always be looking at the API Docs. I found setCodes from here: https://www.soapui.org/apidocs/com/eviware/soapui/security/assertion/ValidHttpStatusCodesAssertion.html