I'm just trying to figure out how Java interop works in karate and looked at examples like here: https://www.swtestacademy.com/java-code-in-karate-api-tests/
My java class is this:
import java.io.*;
import java.lang.Process;
public class Utils {
double m_x;
public Utils(double x) {
m_x = x;
}
public static void main(String[] args) {
System.out.println("Hello World");
}
public static int exec(String command) throws InterruptedException, IOException {
Runtime run = Runtime.getRuntime();
Process proc = run.exec(new String[]{"/bin/bash", "-c", command});
proc.waitFor();
return proc.exitValue();
}
public double nonStatic() {
return m_x;
}
public double nonStatic1(double i) {
return i+1;
}
}
I can call the static methods, but not the constructor or nonStatic methods. BTW, I know Java can handle function overloads, but I'm not sure about JavaScript, so that's why I use different function names.
One of my attempts to call the non-static functions is like this:
Scenario: nonStatic
* def nonStat =
"""
function() {
var Utils = Java.type('Utils');
var obj = new Utils(7.0);
return obj.nonStatic();
}
"""
* def result = call nonStat
* print 'result', result
Scenario: nonStatic(int i)
* def nonStat1 =
"""
function(i) {
var Utils = Java.type('Utils');
var obj = new Utils(7.0)
return obj.nonStatic1(i);
}
"""
* def result = call nonStat1 15
* print 'result1', result
I get: javascript function call failed: :3 TypeError: Can not create new object with constructor Utils with the passed arguments; they do not match any of its method signatures.
I had hoped that a JavaScript number would be converted to a double? The main function and exec function work. Both are static. I can get back a number, but fail at passing a number from karate/js to Java.
Thanks in advance for any help.
Aucun commentaire:
Enregistrer un commentaire