Search code examples
xmlsoapuiassertions

SoapUI Assertion to match field value within 0.05 on either side


I currently have a Assertion Script that matches a value from the response to a set value. see below:

// get the xml response                                             
def response = messageExchange.getResponseContent()                                                 
// parse it                                             
def xml = new XmlSlurper().parseText(response)                                              
// find your node by name                                               
def node = xml.'**'.find { it.name() == 'total-premium' }                                               
// assert                                               
assert node.toString().matches("(0|27.11|0)\\d*"), 'Expected Result: 0 or 27.11 or 0 Actual Result: ' + node    

What I would like it to do is to match between values that are 0.05 below and above. So for this particular script, I need the assertion to be true if the total-premium value is anywhere from 27.06 and 27.16.

At the moment the Assertion code match the numerical value that is in the field total-premium to three values in matches("(0|27.11|0)\\d*").

However, instead of me entering the 11 values total-premium could be I want the line assert node.toString().matches("(0|27.11|0)\\d*"), 'Expected Result: 0 or 27.11 or 0 Actual Result: ' + node to pass even if the value in the field total-premium is 0.05 plus or minus the value that I manually enter into this script. which for this example is 27.11.

For a brief overview, I have ~1000 test cases and I use Excel to create the code and assertions for each test case which I then import into SoapUI. So I have the script match automatically depending on what value the Excel Algorithm inputs.


Solution

  • You could use JUnit public static void assertEquals(double expected, double actual, double delta).

    import org.junit.Assert
    // ... your code goes here ...
    // new assert
    if(node.toDouble() != 0.0)
        Assert.assertEquals(27.11, node.toDouble(), 0.05)