Search code examples
performancecsvgroovyjmeterproperties

Value for a dynamically created property is null


In a setUp thread group I read csv file and create as many properties as I have records in my csv file:

import org.apache.commons.csv.CSVParser  
import org.apache.commons.csv.CSVFormat  

def csvFilePath = "csv.csv";  
def reader = new FileReader(csvFilePath);  
def csvParser = CSVParser.parse(reader, CSVFormat.DEFAULT.withHeader());  

def counter = 1;  

csvParser.records.each { record ->  
    def propertyName = "csvDataProp_${counter++}";  
    def concatenatedLine = record.values().join(',');  

    props.put(propertyName, concatenatedLine);  
}

In a main group I retrieve property values like this (in JSR223 PreProcessor, example for a 1st request):

log.info("Before accessing property - CSV Data: ${props.get("csvDataProp_1")}"); 
def csvData = props.get("csvDataProp_1"); 
log.info("CSV Data: ${csvData}");

if (csvData != null) {  

    def values = csvData.split(',');  
    def testNameColumn = values[0];  
    def testName = "Test:" + testNameColumn;  

    vars.put("testName", testName);  
    vars.put("endpointPath", values[2]);  
    vars.put("encryptedValue", values[3]);  
    vars.put("expStatusCode", values[4]);  
} else {
    log.error("CSV Data is null");  
}

But when I check logs I find: Before accessing property - CSV Data: null. CSV Data: null.

I added a debug post processors in the setUp thread and main thread and I can see all properties with their values, but for some reason when I try to retrieve the values in the code they are null.

Has anybody faced such problem? Could you help me with that, please?


Solution

  • Groovy is a kind of strongly-typed language.

    This expression of yours:

    def propertyName = "csvDataProp_${counter++}";  
    

    produces GStringImpl

    and when you're trying to read it as:

    props.get("csvDataProp_1"); 
    

    you're passing a "normal" String to the function.

    As String and GStringImpl are different beasts you're getting null because the property doesn't exist.

    The easiest solution would be explicit cast of GStringImpl to String like:

    def propertyName = "csvDataProp_${counter++}" as String;
    

    More information: