Forum Discussion

vitsvg's avatar
vitsvg
Occasional Contributor
10 years ago

[Res] addAssertion method returns null for AssertionTestStep

Hi there,

I'm using soap UI Pro 4.6.1. and adding ProXPathContainsAssertion dynamically (or XPathContainsAssertion) to the AssertionTestStep by using the following code:

def testStep = context.testCase.testSteps["NameOfTheStep"];
def assertion = testStep.addAssertion("XPath Match");
assertion.name = "NameForAssertion";

It fails with the java.lang.NullPointerException: Cannot set property 'name' on null object at the last statement. Hence the method of assertion creation (testStep.addAssertion) returns null.
If I use the target test case of a different type (e.g. Http Test Request Step) then it works fine without any issues.
I had once the assertion addition worked for me for the AssertionTestStep by incident and then it started to fail again.

Can you please advise if there are any known issues with this method? Or maybe you can just check that unit-tests are not failing for this method?

Should I add a new assertion in a different way for the Assertion Step compared to HttpTestRequest one?

Full scenario to reproduce the problem is below.

1. Create a new test case
2. Add a new AssertionStep
3. Add a new Groovy Script step
4. In the Groovy Script do the following:
a) get the reference to the previously created AssertionStep (via context.testCase.getTestStepAt(0))
b) try to add an assertion of XPath Match to the assertion step
c) assert if the new assertion is not null - you can use log.info(assertion) which would show if assertion instance is NULL or has a value (in which case it will log the class name).

In my case at the point 4c I'm getting an issue that assertion is NULL and I can't change any properties of the assertion instance.

Please assist.

3 Replies

  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hi,

    Adding assertions to Assertion TestSteps use a different flow as different classes are involved.
    Please see the code below, maybe you can adjust it. It adds one Xpath Match assertion to the specified Assertion TestStep in your TestCase.

    import com.eviware.soapui.config.AssertionEntryConfig
    import com.eviware.soapui.impl.wsdl.teststeps.assertions.support.AssertionEntry
    import com.eviware.soapui.config.AssertionTestStepConfig

    def factoryinstance = AssertionTestStepConfig.Factory.newInstance()
    def testStep = context.testCase.testSteps["Assertion TestStep"];
    def source = context.testCase.testSteps["Test Request"];
    def entry1 = new AssertionEntry(factoryinstance.insertNewEntry(0) , testStep.getTestStep() )
    def parent = null
    def entry = entry1.buildAssertionEntry("XPath Match", "Xpath", false, testStep, source, "Response",parent)
    testStep.addAssertionEntry(entry)


    Regards,

    Giscard
    SmartBear Support
  • vitsvg's avatar
    vitsvg
    Occasional Contributor
    Works perfectly!

    Really appreciate the working code sample!

    Now you can mark it as RESOLVED.