Skip to content

Commit 3e7aa8d

Browse files
committed
Fix selecting Spock methods via MethodSelector
1 parent d96351e commit 3e7aa8d

File tree

7 files changed

+73
-4
lines changed

7 files changed

+73
-4
lines changed

dependencies/dependencies.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,6 @@ dependencies {
3232
api("org.jetbrains.kotlinx:kotlinx-coroutines-core:${versions["kotlinx-coroutines-core"]}")
3333
api("org.mockito:mockito-junit-jupiter:${versions["mockito"]}")
3434
api("biz.aQute.bnd:biz.aQute.bndlib:${versions["bnd"]}")
35+
api("org.spockframework:spock-core:${versions["spock"]}")
3536
}
3637
}

documentation/src/docs/asciidoc/release-notes/release-notes-5.7.0-M1.adoc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ on GitHub.
7777

7878
==== Bug Fixes
7979

80-
* ❓
80+
* The Vintage engine no longer fails when resolving a `MethodSelector` for methods of test
81+
classes that cannot be found via reflection. This allows selecting Spock feature methods
82+
by their source code name even though they have a generated method name in the bytecode.
8183

8284
==== Deprecations and Breaking Changes
8385

gradle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ groovy.version=3.0.2
3939
log4j.version=2.13.1
4040
mockito.version=3.3.3
4141
slf4j.version=1.7.30
42+
spock.version=1.3-groovy-2.5
4243

4344
# Tools
4445
checkstyle.version=8.31

junit-vintage-engine/junit-vintage-engine.gradle.kts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import aQute.bnd.gradle.BundleTaskConvention;
33
plugins {
44
`java-library-conventions`
55
`junit4-compatibility`
6+
`java-test-fixtures`
7+
groovy
68
}
79

810
apply(from = "$rootDir/gradle/testing.gradle.kts")
@@ -17,11 +19,22 @@ dependencies {
1719
api(project(":junit-platform-engine"))
1820
api("junit:junit")
1921

22+
testFixturesApi("org.spockframework:spock-core")
23+
2024
testImplementation(project(":junit-platform-launcher"))
2125
testImplementation(project(":junit-platform-runner"))
2226
testImplementation(project(":junit-platform-testkit"))
2327
}
2428

29+
configurations.all {
30+
resolutionStrategy.eachDependency {
31+
if (requested.group == "org.codehaus.groovy") {
32+
useVersion("2.5.11")
33+
because("Spock is not yet compatible with Groovy 3.x")
34+
}
35+
}
36+
}
37+
2538
tasks {
2639
jar {
2740
withConvention(BundleTaskConvention::class) {

junit-vintage-engine/src/main/java/org/junit/vintage/engine/discovery/MethodSelectorResolver.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import static org.junit.platform.engine.support.discovery.SelectorResolver.Resolution.unresolved;
1616
import static org.junit.vintage.engine.descriptor.VintageTestDescriptor.SEGMENT_TYPE_RUNNER;
1717

18-
import java.lang.reflect.Method;
1918
import java.util.Optional;
2019
import java.util.function.Function;
2120

@@ -74,8 +73,8 @@ private Resolution toResolution(RunnerTestDescriptor parent) {
7473

7574
private Filter toMethodFilter(MethodSelector methodSelector) {
7675
Class<?> testClass = methodSelector.getJavaClass();
77-
Method testMethod = methodSelector.getJavaMethod();
78-
return matchMethodDescription(Description.createTestDescription(testClass, testMethod.getName()));
76+
String methodName = methodSelector.getMethodName();
77+
return matchMethodDescription(Description.createTestDescription(testClass, methodName));
7978
}
8079

8180
private Filter toUniqueIdFilter(RunnerTestDescriptor runnerTestDescriptor, UniqueId uniqueId) {

junit-vintage-engine/src/test/java/org/junit/vintage/engine/VintageTestEngineExecutionTests.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import static org.assertj.core.api.Assertions.assertThat;
1414
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;
15+
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectMethod;
1516
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectUniqueId;
1617
import static org.junit.platform.testkit.engine.EventConditions.abortedWithReason;
1718
import static org.junit.platform.testkit.engine.EventConditions.container;
@@ -84,6 +85,7 @@
8485
import org.junit.vintage.engine.samples.junit4.PlainJUnit4TestCaseWithSingleTestWhichFails;
8586
import org.junit.vintage.engine.samples.junit4.PlainJUnit4TestCaseWithSingleTestWhichIsIgnored;
8687
import org.junit.vintage.engine.samples.junit4.PlainJUnit4TestCaseWithTwoTestMethods;
88+
import org.junit.vintage.engine.samples.spock.SpockTestCaseWithUnrolledAndRegularFeatureMethods;
8789
import org.opentest4j.MultipleFailuresError;
8890

8991
/**
@@ -749,6 +751,37 @@ void executesJUnit4TestCaseWithRunnerWithDuplicateChangingChildDescriptions() {
749751
event(engine(), finishedSuccessfully()));
750752
}
751753

754+
@Test
755+
void executesUnrolledSpockFeatureMethod() {
756+
Class<?> testClass = SpockTestCaseWithUnrolledAndRegularFeatureMethods.class;
757+
var request = LauncherDiscoveryRequestBuilder.request().selectors(
758+
selectMethod(testClass, "unrolled feature for #input")).build();
759+
execute(request).allEvents().assertEventsMatchExactly( //
760+
event(engine(), started()), //
761+
event(uniqueIdSubstring(testClass.getName()), started()), //
762+
event(dynamicTestRegistered("unrolled feature for 23")), //
763+
event(test("unrolled feature for 23"), started()), //
764+
event(test("unrolled feature for 23"), finishedWithFailure()), //
765+
event(dynamicTestRegistered("unrolled feature for 42")), //
766+
event(test("unrolled feature for 42"), started()), //
767+
event(test("unrolled feature for 42"), finishedSuccessfully()), //
768+
event(uniqueIdSubstring(testClass.getName()), finishedSuccessfully()), //
769+
event(engine(), finishedSuccessfully()));
770+
}
771+
772+
@Test
773+
void executesRegularSpockFeatureMethod() {
774+
Class<?> testClass = SpockTestCaseWithUnrolledAndRegularFeatureMethods.class;
775+
var request = LauncherDiscoveryRequestBuilder.request().selectors(selectMethod(testClass, "regular")).build();
776+
execute(request).allEvents().assertEventsMatchExactly( //
777+
event(engine(), started()), //
778+
event(container(testClass), started()), //
779+
event(test("regular"), started()), //
780+
event(test("regular"), finishedSuccessfully()), //
781+
event(container(testClass), finishedSuccessfully()), //
782+
event(engine(), finishedSuccessfully()));
783+
}
784+
752785
private static EngineExecutionResults execute(Class<?> testClass) {
753786
return execute(request(testClass));
754787
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.junit.vintage.engine.samples.spock
2+
3+
import spock.lang.Specification
4+
import spock.lang.Unroll
5+
6+
class SpockTestCaseWithUnrolledAndRegularFeatureMethods extends Specification {
7+
8+
@Unroll
9+
def "unrolled feature for #input"() {
10+
expect:
11+
input == 42
12+
where:
13+
input << [23, 42]
14+
}
15+
16+
def "regular"() {
17+
expect:
18+
true
19+
}
20+
}

0 commit comments

Comments
 (0)