Skip to content

Instantly share code, notes, and snippets.

@ysb33r
Last active June 26, 2023 23:43
Show Gist options
  • Save ysb33r/5825457 to your computer and use it in GitHub Desktop.
Save ysb33r/5825457 to your computer and use it in GitHub Desktop.
Running Groovy unittests from the command-line
class Example {
def hhg() { 42 }
}
import spock.lang.*
class ExampleSpec extends Specification {
def "It must be 42" () {
def ex = new Example()
expect:
ex.hhg() == 6*9
}
}
import static org.junit.Assert.*
import org.junit.*
class ExampleTest {
@Test
void isIt42() {
def ex = new Example()
assertTrue ex.hhg() == 6*9
}
}
# Compile the groovy code
groovyc *.groovy
# Running a JUnit4 test
java -cp $GROOVY_HOME/embeddable/groovy-all-2.1.4.jar:$GROOVY_HOME/lib/junit-4.11.jar:$GROOVY_HOME/lib/hamcrest-core-1.3.jar:. \
org.junit.runner.JUnitCore ExampleTest
# Download Spock if you don't have it
grape install org.spockframework spock-core 0.7-groovy-2.0
# Running a Spock class
java -cp ~/.grapes/org.spockframework/spock-core/jars/spock-core-0.7-groovy-2.0.jar:$GROOVY_HOME/embeddable/groovy-all-2.1.4.jar:$GROOVY_HOME/lib/junit-4.11.jar:$GROOVY_HOME/lib/hamcrest-core-1.3.jar:. \
org.junit.runner.JUnitCore ExampleSpec
@JonJagger
Copy link

Ok. Thanks. I now have something that works.

groovyc *.groovy
if [ $? -eq 0 ]; then
  groovy HikerSpec
fi

But it still hard-wires the name HikerSpec.
Do you know if that can be fixed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment