I have a class called JsPlayer
that contains a JS script in its fields and has methods that execute functions from that script by name, using ScriptEngine
. I'd like to have a test that verifies that when I call player.onReinforcement
the relevant function in the javascript is called with the correct parameters.
@Test
public void playerReinforcementGetsBoardFromGame(){
String script = "function " + REINFORCEMENT_JS_FUNCTION_NAME + "(board, soldiers) { return []; }";
Player player = new JsPlayer(script);
player.onReinforcement(board, 0);
// verify REINFORCEMENT_JS_FUNCTION_NAME(board, 0) called
}
Here's the simplified method in JsPlayer
:
@Override
public Iterable<ReinforcementMove> onReinforcement(Board board, int reinforcement) {
try {
JSObject result = (JSObject) getInvocableJSEngine().invokeFunction(REINFORCEMENT_JS_FUNCTION_NAME, board, reinforcement);
return extractMovesFromJSResult(result, ReinforcementMove[].class);
} catch (ScriptException | NoSuchMethodException e) {
}
}
Since the code inside the script runs entirely inside the engine, I can't use classic Mockito verify to solve this. I don't know how to verify that the javascript really received the parameters I sent to it.
NOTE: I can't write a script onReinforcement(board, reinforcement){return board;}
, because the method extractMovesFromJSResult
expects a specific return type.
Aucun commentaire:
Enregistrer un commentaire