Last active
June 26, 2023 23:43
-
-
Save ysb33r/5825457 to your computer and use it in GitHub Desktop.
Running Groovy unittests from the command-line
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Example { | |
def hhg() { 42 } | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import spock.lang.* | |
class ExampleSpec extends Specification { | |
def "It must be 42" () { | |
def ex = new Example() | |
expect: | |
ex.hhg() == 6*9 | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static org.junit.Assert.* | |
import org.junit.* | |
class ExampleTest { | |
@Test | |
void isIt42() { | |
def ex = new Example() | |
assertTrue ex.hhg() == 6*9 | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
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.
@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.
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
Add this to your spec:
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 justassert
for an improved power assert error message in your JUnit test.