Skip to content

Commit b71e3d7

Browse files
authored
Add spring7 tests (#2262)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added a Spring 7 test module with Java 17 toolchain and new tests verifying Spring integration and runtime compatibility. * **Bug Fixes** * Broadened detection of injection/resource annotations to include Jakarta variants to avoid false-positive shared-field errors. * **Chores** * Expanded Spring/Java compatibility matrix, added Jakarta injection API, standardized dependency notation, disabled certain test compilation tasks, and included the new test module in the build. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 8d380cc commit b71e3d7

19 files changed

Lines changed: 240 additions & 14 deletions

settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ if (variant == 2.5) {
104104
// require Java 17 which isn't supported by Groovy 2.5
105105
include "spock-spring:boot3-test"
106106
include "spock-spring:spring6-test"
107+
include "spock-spring:spring7-test"
107108
}
108109

109110
// https://issues.apache.org/jira/projects/TAP5/issues/TAP5-2588

spock-spring/spring.gradle

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ def springVersion = "4.3.5.RELEASE"
1313
def testSpringVersions = [
1414
(springVersion): (8..11),
1515
"5.0.2.RELEASE": (8..11),
16-
"6.0.0" : (17..17)
16+
"6.0.0" : (17..21),
17+
"7.0.6" : (17..javaVersions.max())
1718
].findAll { javaVersion in it.value }.keySet()
1819

1920
dependencies {
@@ -29,6 +30,7 @@ testing {
2930
useJUnitJupiter()
3031
dependencies {
3132
implementation "javax.inject:javax.inject:1"
33+
implementation "jakarta.inject:jakarta.inject-api:2.0.1"
3234
implementation groovylibs.groovySql // for groovy.sql.Sql
3335
implementation libs.junit.platform.testkit
3436
runtimeOnly libs.h2database
@@ -95,10 +97,15 @@ testing {
9597

9698
["compileTestJava", "compileTestGroovy"].each { taskName ->
9799
tasks.named(taskName).configure {
98-
onlyIf { false } //skip normal test task
100+
enabled = false //skip normal test task
99101
}
100102
}
101103

104+
tasks.withType(GroovyCompile).configureEach {
105+
// this is needed for Spring 7 bean names from parameter names
106+
groovyOptions.parameters = true
107+
}
108+
102109
tasks.named("jar", Jar) {
103110
manifest {
104111
attributes(

spock-spring/spring3-test/spring3-test.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ dependencies {
55

66
testImplementation projects.spockCore
77
testImplementation "org.springframework:spring-context"
8-
testImplementation ("org.springframework:spring-test")
8+
testImplementation "org.springframework:spring-test"
99

1010
testRuntimeOnly projects.spockSpring
1111
}

spock-spring/spring5-test/spring5-test.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ dependencies {
77
testImplementation projects.spockSpring
88
testImplementation libs.junit4
99
testImplementation "org.springframework:spring-context"
10-
testImplementation ("org.springframework:spring-test")
10+
testImplementation "org.springframework:spring-test"
1111

1212
}
1313

spock-spring/spring6-test/spring6-test.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ dependencies {
2020
testImplementation projects.spockSpring
2121
testImplementation libs.junit4
2222
testImplementation "org.springframework:spring-context"
23-
testImplementation("org.springframework:spring-test")
23+
testImplementation "org.springframework:spring-test"
2424

2525
}
2626

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
def springVersion = "7.0.6"
2+
3+
java {
4+
toolchain {
5+
languageVersion = JavaLanguageVersion.of(17)
6+
}
7+
}
8+
9+
tasks.withType(JavaCompile).configureEach {
10+
javaCompiler = javaToolchains.compilerFor {
11+
languageVersion = JavaLanguageVersion.of(17)
12+
}
13+
options.encoding = 'UTF-8'
14+
}
15+
16+
dependencies {
17+
implementation "org.springframework:spring-core"
18+
19+
testImplementation projects.spockCore
20+
testImplementation projects.spockSpring
21+
testImplementation libs.junit4
22+
testImplementation "org.springframework:spring-context"
23+
testImplementation "org.springframework:spring-test"
24+
25+
}
26+
27+
28+
configurations.all {
29+
resolutionStrategy.eachDependency {
30+
if (requested.group == "org.springframework" ) {
31+
useVersion(springVersion)
32+
}
33+
}
34+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.spockframework.spring7
2+
3+
import groovy.transform.CompileStatic
4+
import org.springframework.context.annotation.Bean
5+
6+
import java.util.concurrent.Callable
7+
import java.util.concurrent.ExecutorService
8+
9+
@CompileStatic
10+
class NoMockConfig {
11+
12+
@Bean
13+
ExecutorService executor() {
14+
throw new RuntimeException("This should not be called")
15+
}
16+
17+
@Bean
18+
ServiceExecutor serviceExecutor(ExecutorService executorService) {
19+
return new ServiceExecutor(executorService)
20+
}
21+
22+
23+
}
24+
25+
class ServiceExecutor {
26+
private final ExecutorService executorService
27+
28+
ServiceExecutor(ExecutorService executorService) {
29+
this.executorService = executorService
30+
}
31+
32+
def exec() {
33+
executorService.submit({"done"} as Callable).get()
34+
}
35+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.spockframework.spring7
2+
3+
import org.springframework.beans.factory.annotation.Autowired
4+
import org.springframework.test.context.ContextConfiguration
5+
import spock.lang.Specification
6+
7+
import java.util.concurrent.Executor
8+
9+
@ContextConfiguration(classes = TestConfig)
10+
class RuntimeCompatibilitySpec extends Specification {
11+
12+
@Autowired
13+
Executor injectMe
14+
15+
def "no runtime errors are thrown"() {
16+
expect:
17+
1 == 1
18+
}
19+
20+
def "injection works"() {
21+
expect:
22+
injectMe != null
23+
}
24+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.spockframework.spring7
2+
3+
import org.spockframework.spring.SpringBean
4+
import org.springframework.beans.factory.annotation.Autowired
5+
import org.springframework.test.context.ContextConfiguration
6+
import spock.lang.Specification
7+
8+
import java.util.concurrent.ExecutorService
9+
import java.util.concurrent.Future
10+
11+
@ContextConfiguration(classes = NoMockConfig)
12+
class SpringBeanTest extends Specification {
13+
14+
@Autowired
15+
ServiceExecutor serviceExecutor
16+
17+
@SpringBean
18+
ExecutorService executor = Mock()
19+
20+
def "replace executor"() {
21+
when:
22+
def result = serviceExecutor.exec()
23+
24+
then:
25+
result == 'mocked'
26+
1 * executor.submit(_) >> Stub(Future) {
27+
get() >> 'mocked'
28+
}
29+
}
30+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.spockframework.spring7;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.springframework.test.context.ContextConfiguration;
7+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
8+
9+
/**
10+
* Make sure we still can correctly execute tests executed by the SpringRunner that are not spock tests
11+
*/
12+
@RunWith(SpringJUnit4ClassRunner.class)
13+
@ContextConfiguration
14+
public class SpringRunnerTest {
15+
16+
@Test
17+
public void testThatSpringRunnerExecutesCorrectly() {
18+
Assert.assertTrue(true);
19+
}
20+
21+
}

0 commit comments

Comments
 (0)