Search code examples
javascriptjavasqlkarate

in Karate DSL, how do you escape single quotes when passing a variable within a java argument call


I'm trying to add making calls to a database as part of an end to end effort with the API project at work. How would I escape single quotes within a variable that is being passed as an argument within an assert method. I have tried the following and it did not work.

assert JavaClass.executeSQLQuery('SELECT COUNT(*) As Result FROM PartType WHERE reference = 'evaluate';', '1') == true

ERROR is that it cannot evaluate the single quotes within the value

Other attempts:

def sqlQuery = 
    
    """
    SELECT COUNT(*) As Result 
    FROM PartType 
    WHERE reference = 'updateerferencee';
    """

assert JavaMethods.executeSQLQuery(sqlQuery, '1') == true

ERROR: See attachmententer image description here


Solution

  • Through my novice programming skills, I realized I was trying to call a Java method that is being evaluated as JavaScript and the whole thing is baked into the Karate mini-language (which confused the s*** out of me). The answer has been tested and working below:

    assert JavaClass.executeSQLQuery('SELECT COUNT(*) As Result FROM PartType WHERE reference = \'evaluate\';', '1') == true