Skip to content

Commit 95d433d

Browse files
ChoosecheeAndreasTuleonard84
authored
Fix module testing not working due to call to internal JUnit API (#2187)
Replaced the use of the internal JUnit class `org.junit.platform.commons.util.ClassUtils` with a new custom `org.spockframework.util.ClassUtil` class. fixes #1227 --------- Co-authored-by: Andreas Turban <[email protected]> Co-authored-by: Leonard Brünings <[email protected]>
1 parent b8f0f84 commit 95d433d

File tree

4 files changed

+91
-2
lines changed

4 files changed

+91
-2
lines changed

docs/release_notes.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ will now require that the spec is annotated with <<parallel_execution.adoc#isola
6363
* Fix handling of condition method calls and Groovy `with()` method spockPull:2162[]
6464
** This could break tests using `with()` which relied on the bug in the past spockIssue:2269[]
6565
** Use the Spock `with(yourObject)` instead of `yourObject.with()` or prefix it `!!` to fix your test
66+
* Fix module testing not working due to call to JUnit internal API spockPull:2187[]
67+
** This also fixes the usage of Spock with JUnit 6 in OSGi environments
6668

6769
== 2.4-M7 (2025-11-23)
6870

spock-core/src/main/java/org/spockframework/runtime/SpockNode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
import org.spockframework.runtime.model.*;
44
import org.spockframework.runtime.model.parallel.ResourceAccessMode;
5+
import org.spockframework.util.ClassUtil;
56
import org.spockframework.util.ExceptionUtil;
67
import spock.config.RunnerConfiguration;
78

89
import java.util.*;
910
import java.util.stream.Collectors;
1011

11-
import org.junit.platform.commons.util.ClassUtils;
1212
import org.junit.platform.engine.*;
1313
import org.junit.platform.engine.support.descriptor.*;
1414
import org.junit.platform.engine.support.hierarchical.*;
@@ -91,7 +91,7 @@ public Set<ExclusiveResource> getExclusiveResources() {
9191
protected static MethodSource featureToMethodSource(FeatureInfo info) {
9292
return MethodSource.from(info.getSpec().getBottomSpec().getReflection().getName(),
9393
info.getName(),
94-
ClassUtils.nullSafeToString(info.getFeatureMethod().getReflection().getParameterTypes()) // TODO replace internal API
94+
ClassUtil.nullSafeToString(info.getFeatureMethod().getReflection().getParameterTypes())
9595
);
9696
}
9797

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.spockframework.util;
18+
19+
import java.util.Arrays;
20+
21+
import static java.util.stream.Collectors.joining;
22+
23+
/**
24+
* Utility methods for working with {@link Class} objects.
25+
*
26+
* @since 2.4
27+
*/
28+
public class ClassUtil {
29+
public static String nullSafeToString(@Nullable Class<?> clazz) {
30+
return clazz == null ? "null" : clazz.getName();
31+
}
32+
33+
public static String nullSafeToString(@Nullable Class<?>... clazzes) {
34+
if (clazzes == null) return "";
35+
36+
return Arrays.stream(clazzes)
37+
.map(ClassUtil::nullSafeToString)
38+
.collect(joining(", "));
39+
}
40+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.spockframework.util
18+
19+
import spock.lang.Specification
20+
21+
class ClassUtilSpec extends Specification {
22+
def "get string representation of class"() {
23+
expect:
24+
ClassUtil.nullSafeToString(clazz as Class<?>) == expectedString
25+
26+
where:
27+
clazz | expectedString
28+
Set | "java.util.Set"
29+
Tuple | "groovy.lang.Tuple"
30+
ClassUtil | "org.spockframework.util.ClassUtil"
31+
null | "null"
32+
}
33+
34+
def "get string representation of none, one, or multiple potentially null classes"(Class<?>[] clazzes, String expectedString) {
35+
expect: "result is names from single class overload separated by a comma and space each"
36+
ClassUtil.nullSafeToString(clazzes as Class<?>[]) == expectedString
37+
38+
where:
39+
clazzes | expectedString
40+
[List, Set, Queue, Map] | "java.util.List, java.util.Set, java.util.Queue, java.util.Map"
41+
[File, null, URL] | "java.io.File, null, java.net.URL"
42+
[null, null] | "null, null"
43+
[File] | "java.io.File"
44+
[] | ""
45+
null | ""
46+
}
47+
}

0 commit comments

Comments
 (0)