Forum Discussion

krispokkuluri's avatar
krispokkuluri
Occasional Contributor
3 years ago

How do we handle empty string dynamically in assertions in ReadyAPI

ERROR: An error occurred [empty String], see error log for details Hi i have a need to validate two data sources using nested loop on multiple records in several tables Steps Build a Rest Request ...
  • ChrisAdams's avatar
    ChrisAdams
    3 years ago

    Hi,

     

    The below example and function show how you can test for an empty string.  Just create a brand new Groovy script step, paste in the contents and click Go in the Groovy window.  Ensure you can see the logging for the answers....

     

    def value = "1.12";
    log.info("value ${value} is empty?  ${ isEmpty(value)}");
    
    value = "1";
    log.info("value ${value} is empty?  ${ isEmpty(value)}");
    
    value = "";
    log.info("value ${value} is empty?  ${ isEmpty(value)}");
    
    value = null;
    log.info("value ${value} is empty?  ${ isEmpty(value)}");
    
    def isEmpty(stringvalue) {
    
        if(stringvalue == null) {
        	    return true;
        } else {
    		if((stringvalue.length() == 0) ||
    			(stringvalue == "") ) {
    			return true;
    		} else {
    			return false;
    		}
    	}
    }

     

    Also, Groovy Double does not have a tryParse function unlike other languages, but it does throw an exception which might help...  Next example...

    def myValue = "1.12";
    log.info("is ${myValue} parsable as double?  ${canBeDouble(myValue)}");
    
    myValue = "1";
    log.info("is ${myValue} parsable as double?  ${canBeDouble(myValue)}");
    
    myValue = "abc";
    log.info("is ${myValue} parsable as double?  ${canBeDouble(myValue)}");
    
    def canBeDouble(theValue) {
    	try {
    		  def a = Double.parseDouble(theValue);
    		  return true;
    	} catch (NumberFormatException e) {
    		  //the parseDouble failed and you need to handle it here
    		  return false;
    	}
    }

     

    You can then maybe use these examples, or modify them, to test suspect values from the db before trying the assert.