2014-05-09 15 views
6

Il seguente test deve passare, ma non è cosìCome usare ScriptEngine in ScalaTest

class EngineTest extends FunSuite { 

    test("engine should not be null") { 
    val manager: ScriptEngineManager = new ScriptEngineManager 
    val engine: ScriptEngine = manager.getEngineByName("nashorn") 
    assert(engine != null) 
    } 
} 

La manager.getEngineFactories() sembra essere vuoto. Perché? Come inizializzare il contesto?

+0

reso il passaggio di test nell'IDE ma non SBT dalle coperture. Mi chiedo che cosa è diverso. – jonbros

+0

il classpath è molto probabilmente il problema – aepurniet

+0

http://stackoverflow.com/q/10054252/1296806 per la distro-dipendenza del supporto per il rinoceronte. –

risposta

3

Quali versioni stai utilizzando? Questo è sbt .13.

> console 
[info] Starting scala interpreter... 
[info] 
Welcome to Scala version 2.11.0 (OpenJDK 64-Bit Server VM, Java 1.7.0_25). 
Type in expressions to have them evaluated. 
Type :help for more information. 

scala> import javax.script._ 
import javax.script._ 

scala> new ScriptEngineManager().getEngineByName("scala") 
res0: javax.script.ScriptEngine = [email protected] 

scala> new ScriptEngineManager().getEngineByName("rhino") 
res1: javax.script.ScriptEngine = com.sun.[email protected] 

scala> new ScriptEngineManager().getEngineFactories 
res2: java.util.List[javax.script.ScriptEngineFactory] = [[email protected], [email protected]] 

Aspetta, ti ha chiesto sul contesto di prova -

Bene, prima ho perso interesse per la decodifica più SBT, aggiungendo al libraryDependencies:

"org.scala-lang" % "scala-compiler" % scalaVersion.value % "test", 

consente la localizzazione del motore di script Scala:

@Test def engines: Unit = { 
    import javax.script._ 
    val all = new ScriptEngineManager().getEngineFactories 
    Console println s"Found ${all.size}: $all" 
    assert(all.size > 0) 
    } 

Senza dubbio c'è un modo semplice per aggiungere runtime: full-classpath da testare: f ull-classpath direttamente. Perché è il semplice strumento di costruzione, giusto?

Per Nashorn su Java 8, prendere nota del percorso:

> set fullClasspath in Test += Attributed.blank(file(s"${util.Properties.javaHome}/lib/ext/nashorn.jar")) 
[info] Defining test:fullClasspath 
[info] The new value will be used by test:console, test:executeTests and 5 others. 
[info] Run `last` for details. 
[info] Reapplying settings... 
[info] Set current project to goofy (in build file:/home/apm/goofy/) 
> test 
Found 1: [[email protected]] 
[info] Passed: Total 10, Failed 0, Errors 0, Passed 10 

Aggiornamento: https://github.com/sbt/sbt/issues/1214

anche I guess it's still considered black art:

// Somehow required to get a js engine in tests (https://github.com/sbt/sbt/issues/1214) 

fork in Test := true 
+0

Stavo solo supponendo: '> set (fullClasspath in Test) ++ = (fullClasspath in Runtime) .value' ma quelli sono solo deps. –

+0

Penso di aver imparato qualcosa riguardo a SBT qui. Grazie molto! – jonbros

4

È necessario passare esplicitamente in un ClassLoader al costruttore ScriptEngineManager . Se non lo fai, utilizza Thread.currentThread().getContextClassLoader() che è impostato su qualcosa di strano quando si esegue in SBT. Passiamo semplicemente nel null nel nostro codice per farlo funzionare. È inoltre possibile passare in getClass.getClassLoader:

class EngineTest extends FunSuite { 
    test("engine should not be null - null classloader") { 
    val manager: ScriptEngineManager = new ScriptEngineManager(null) 
    val engine: ScriptEngine = manager.getEngineByName("nashorn") 
    assert(engine != null) 
    } 

    test("engine should not be null - getClass.getClassLoader classloader") { 
    val manager: ScriptEngineManager = new ScriptEngineManager(getClass.getClassLoader) 
    val engine: ScriptEngine = manager.getEngineByName("nashorn") 
    assert(engine != null) 
    } 
} 

Entrambi i test passano per me:

[info] EngineTest: 
[info] - engine should not be null - null classloader 
[info] - engine should not be null - getClass.getClassLoader classloader 
[info] Run completed in 186 milliseconds. 
[info] Total number of tests run: 2 
[info] Suites: completed 1, aborted 0 
[info] Tests: succeeded 2, failed 0, canceled 0, ignored 0, pending 0 
[info] All tests passed. 
Problemi correlati