Javascript Validation in Java 15+ JUnit Test

231 Views Asked by At

Now that Nashorn has been deprecated & removed in JDK 15 what is the best approach to validate that a String is valid Javascript in a JUnit test?

Previous Code:

    private void validateJavascript(final String js) throws ScriptException {
        final NashornScriptEngine scriptEngine = (NashornScriptEngine) new NashornScriptEngineFactory().getScriptEngine();
        final CompiledScript compile = scriptEngine.compile(js);
    }
1

There are 1 best solutions below

0
Patrick Bray On

Found the following similar thread Different Nashorn engine for Java < 15 and >= 15? which pointed me to standalone implementation which can be added as a test dependency:

testImplementation 'org.openjdk.nashorn:nashorn-core:15.2'

And then instantiated as follows:

final NashornScriptEngine scriptEngine = (NashornScriptEngine) new NashornScriptEngineFactory().getScriptEngine();