Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kriegaex/2866f56a0ecde17d65ea03f23018f0d0 to your computer and use it in GitHub Desktop.
Save kriegaex/2866f56a0ecde17d65ea03f23018f0d0 to your computer and use it in GitHub Desktop.
How to run Spock 2 tests via JUnit 5 stand-alone console runner (please adjust variables, paths, separators etc. by yourself)
export MAVEN_REPO=c:/Users/alexa/.m2/repository
export JUNIT5_STANDALONE=$MAVEN_REPO/org/junit/platform/junit-platform-console-standalone/1.6.2/junit-platform-console-standalone-1.6.2.jar
export GROOVY3=$MAVEN_REPO/org/codehaus/groovy/groovy/3.0.4/groovy-3.0.4.jar
export SPOCK2_CORE=$MAVEN_REPO/org/spockframework/spock-core/2.0-M3-groovy-3.0/spock-core-2.0-M3-groovy-3.0.jar
java -jar $JUNIT5_STANDALONE -cp "$GROOVY3;$SPOCK2_CORE;target/test-classes;target/classes" --disable-banner --details=verbose --select-class de.scrum_master.testing.MyTest
@kriegaex
Copy link
Author

kriegaex commented Jul 9, 2020

From a conversation on the Spock channel on Gitter:

I never tried this before because I always run my test from Maven, but I was curious, checked the JUnit 5 documentation and then did this with one of my sample projects (syntax is Git Bash under Windows, please adjust path styles, path separators and the way you use variables if you use cmd.exe or a normal UNIX shell):

(the script which is now the Gist)

The output is like this:

Test plan execution started. Number of static tests: 1
.
+-- JUnit Jupiter
'-- JUnit Jupiter finished after 8 ms.
+-- JUnit Vintage
'-- JUnit Vintage finished after 1 ms.
+-- Spock
| +-- MyTest
| | +-- test
| | |      tags: []
| | |  uniqueId: [engine:spock]/[spec:de.scrum_master.testing.MyTest]/[feature:$spock_feature_0_0]
| | |    parent: [engine:spock]/[spec:de.scrum_master.testing.MyTest]
| | |    source: MethodSource [className = 'de.scrum_master.testing.MyTest', methodName = 'test', methodParameterTypes = '']
| | |  duration: 134 ms
| | |    status: [OK] SUCCESSFUL
| '-- MyTest finished after 138 ms.
'-- Spock finished after 322 ms.
Test plan execution finished. Number of all tests: 1

Test run finished after 372 ms
[         4 containers found      ]
[         0 containers skipped    ]
[         4 containers started    ]
[         0 containers aborted    ]
[         4 containers successful ]
[         0 containers failed     ]
[         1 tests found           ]
[         0 tests skipped         ]
[         1 tests started         ]
[         0 tests aborted         ]
[         1 tests successful      ]
[         0 tests failed          ]

Git Bash also supports ANSI colours, under cmd.exe you maybe want to use --disable-ansi-colors.

In the console log you can see that JUnit detects multiple engines: Jupiter, Vintage and Spock. If you only want to run Spock tests, use -e spock.

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