EmbeddedSpecRunner with Unroll

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

EmbeddedSpecRunner with Unroll

Vinícius Pereira
Hi everyone,

I'm new with Groovy/Spock and sorry if my question is dumb.

I want to test some Nashorn JS code using Spock. So far so good.
My problem is that I do not have a .class to verified against the test code. So I used the EmbeddedSpecRunner to compile and execute the specs in memory:

import spock.util.EmbeddedSpecRunner

def
esr = new EmbeddedSpecRunner()
esr
.runWithImports("""
import spock.lang.Shared
import spock.lang.Specification
import spock.lang.Unroll

import javax.script.ScriptEngine
import javax.script.ScriptEngineManager

class TestSpec extends Specification {
    @Shared ScriptEngine nashorn = new ScriptEngineManager().getEngineByName("nashorn")

    def setupSpec() {
        nashorn.eval("\""
        var print = function (name) {
    var myName = new java.lang.String();
    myName = name;
    return myName;
}
        "\""
        )
    }

    @Unroll
    def "compare between #a and #expected"() {
        when:
        def result = nashorn.invokeFunction('print',a)
        then:
        result == expected
        where:
        a | expected
        "Vinicius" | "Vinícius"
        "Thiago" | "Tiago"
    }
}
""")

After this I create another Groovy Script just to run it:

import java.nio.file.Paths

def binding = new Binding()
def file = Paths.get(".", "src/test/groovy/spocknashorn").normalize().toUri()
def engine = new GroovyScriptEngine(file.toString())

def greeter = engine.run('EmbeddedClassLoader.groovy', binding)


If I run the script above, than the Spec at Unroll is called twice (ok) but with the same arg ("Vinicius"). The "Thiago" is never called. Why?


Following, I need to call the script from inside a java code. So I create a simple one. This time, the Spec is called just once just with the first arg ("Vinicius"):

public class SomeJavaClass {
   
public static void main (String[] args) throws Exception {
       
Runner runner = new Runner();
        runner
.run();
   
}
}

Why Unroll does not work properly at Groovy Script and Java code? What is wrong?

Thank you for your time =)

--
You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/spockframework.
For more options, visit https://groups.google.com/d/optout.