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

Hi Shalk.
First off thanks for the gist above. Was super useful getting groovy+spock installed into cyber-dojo.
I'm now trying to upgrade to groovy 3.0.3 +spock.
I've downloaded spock-core-2.0-M2-groovy-3.0.jar from https://search.maven.org/search?q=g:org.spockframework
I have compiled the groovy source and tests ok.
However, when I try to run the tests I'm getting a junit failure
"org.junit.runners.model.InvalidTestClassError: Invalid test class 'HikerSpec':\n",
" 1. No runnable methods\n",

If you can shed any light on this I would very much appreciate it.

My command looks like this:
GROOVY_HOME=/groovy-3.0.3
SPOCK_JAR=/spock/spock-core-2.0-M2-groovy-3.0.jar
CP=${SPOCK_JAR}
CP=${CP}:${GROOVY_HOME}/indy/groovy-3.0.3-indy.jar
CP=${CP}:${GROOVY_HOME}/lib/junit-4.13.jar
CP=${CP}:${GROOVY_HOME}/lib/hamcrest-core-1.3.jar
CP=${CP}:.
java -cp ${CP} org.junit.runner.JUnitCore HikerSpec

@ysb33r
Copy link
Author

ysb33r commented May 1, 2020

@JonJagger I'll see if I can get around to this on the weekend.

@paulk-asert
Copy link

paulk-asert commented May 1, 2020

Add this to your spec:

@Grab('org.spockframework:spock-core:2.0-M2-groovy-3.0')
@GrabExclude('org.codehaus.groovy:*')

Then just run (I used 3.0.3):
> groovy ExampleSpec
You can also run your test directly (without change or classpath setup):
> groovy ExampleSpec
And you might consider replacing assertTrue with just assert for an improved power assert error message in your JUnit test.

@JonJagger
Copy link

Thanks Paul.
Does @grab try to pull the jar over the network?
If so then its not an option since this is all running inside a docker container with no network access.
I can't see any difference between your two groovy calls...?
Is there a way Groovy can find the names of the Spec classes by itself?
I'd prefer it if it still worked if people changed the names of their Spec classes.

@paulk-asert
Copy link

@grab doesn't have to use a network if you have some local cache/repo. You can also add spock jar(s) to a ${user.home}/.groovy/lib or ${groovy.home}/lib directory.

@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