-
Notifications
You must be signed in to change notification settings - Fork 479
Add spring7 tests #2262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add spring7 tests #2262
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| def springVersion = "7.0.6" | ||
|
|
||
| java { | ||
| toolchain { | ||
| languageVersion = JavaLanguageVersion.of(17) | ||
| } | ||
| } | ||
|
|
||
| tasks.withType(JavaCompile).configureEach { | ||
| javaCompiler = javaToolchains.compilerFor { | ||
| languageVersion = JavaLanguageVersion.of(17) | ||
| } | ||
| options.encoding = 'UTF-8' | ||
| } | ||
|
|
||
| dependencies { | ||
| implementation "org.springframework:spring-core" | ||
|
|
||
| testImplementation projects.spockCore | ||
| testImplementation projects.spockSpring | ||
| testImplementation libs.junit4 | ||
| testImplementation "org.springframework:spring-context" | ||
| testImplementation "org.springframework:spring-test" | ||
|
|
||
| } | ||
|
|
||
|
|
||
| configurations.all { | ||
| resolutionStrategy.eachDependency { | ||
| if (requested.group == "org.springframework" ) { | ||
| useVersion(springVersion) | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package org.spockframework.spring7 | ||
|
|
||
| import groovy.transform.CompileStatic | ||
| import org.springframework.context.annotation.Bean | ||
|
|
||
| import java.util.concurrent.Callable | ||
| import java.util.concurrent.ExecutorService | ||
|
|
||
| @CompileStatic | ||
| class NoMockConfig { | ||
|
|
||
| @Bean | ||
| ExecutorService executor() { | ||
| throw new RuntimeException("This should not be called") | ||
| } | ||
|
|
||
| @Bean | ||
| ServiceExecutor serviceExecutor(ExecutorService executorService) { | ||
| return new ServiceExecutor(executorService) | ||
| } | ||
|
|
||
|
|
||
| } | ||
|
|
||
| class ServiceExecutor { | ||
| private final ExecutorService executorService | ||
|
|
||
| ServiceExecutor(ExecutorService executorService) { | ||
| this.executorService = executorService | ||
| } | ||
|
|
||
| def exec() { | ||
| executorService.submit({"done"} as Callable).get() | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package org.spockframework.spring7 | ||
|
|
||
| import org.springframework.beans.factory.annotation.Autowired | ||
| import org.springframework.test.context.ContextConfiguration | ||
| import spock.lang.Specification | ||
|
|
||
| import java.util.concurrent.Executor | ||
|
|
||
| @ContextConfiguration(classes = TestConfig) | ||
| class RuntimeCompatibilitySpec extends Specification { | ||
|
|
||
| @Autowired | ||
| Executor injectMe | ||
|
|
||
| def "no runtime errors are thrown"() { | ||
| expect: | ||
| 1 == 1 | ||
| } | ||
|
|
||
| def "injection works"() { | ||
| expect: | ||
| injectMe != null | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package org.spockframework.spring7 | ||
|
|
||
| import org.spockframework.spring.SpringBean | ||
| import org.springframework.beans.factory.annotation.Autowired | ||
| import org.springframework.test.context.ContextConfiguration | ||
| import spock.lang.Specification | ||
|
|
||
| import java.util.concurrent.ExecutorService | ||
| import java.util.concurrent.Future | ||
|
|
||
| @ContextConfiguration(classes = NoMockConfig) | ||
| class SpringBeanTest extends Specification { | ||
|
|
||
| @Autowired | ||
| ServiceExecutor serviceExecutor | ||
|
|
||
| @SpringBean | ||
| ExecutorService executor = Mock() | ||
|
|
||
| def "replace executor"() { | ||
| when: | ||
| def result = serviceExecutor.exec() | ||
|
|
||
| then: | ||
| result == 'mocked' | ||
| 1 * executor.submit(_) >> Stub(Future) { | ||
| get() >> 'mocked' | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package org.spockframework.spring7; | ||
|
|
||
| import org.junit.Assert; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.springframework.test.context.ContextConfiguration; | ||
| import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
|
|
||
| /** | ||
| * Make sure we still can correctly execute tests executed by the SpringRunner that are not spock tests | ||
| */ | ||
| @RunWith(SpringJUnit4ClassRunner.class) | ||
| @ContextConfiguration | ||
| public class SpringRunnerTest { | ||
|
Comment on lines
+12
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
# Search for any context configuration files related to SpringRunnerTest
fd -t f "SpringRunnerTest" spock-spring/spring7-test/
echo "---"
# Also search for any -context.xml files in spring7-test
fd -t f "\-context\.xml$" spock-spring/spring7-test/
echo "---"
# Check what files exist in the test directory structure
fd -t f "\.xml$|\.java$|\.groovy$" spock-spring/spring7-test/src/test | head -20Repository: spockframework/spock Length of output: 621 🏁 Script executed: #!/bin/bash
# Read the SpringRunnerTest.java file to see full class content
cat -n spock-spring/spring7-test/src/test/groovy/org/spockframework/spring7/SpringRunnerTest.javaRepository: spockframework/spock Length of output: 770 🏁 Script executed: #!/bin/bash
# Check how other tests configure their Spring context
rg -A 3 "@ContextConfiguration|@SpringBootTest" spock-spring/spring7-test/src/test/groovy/org/spockframework/spring7/ -t groovy -t javaRepository: spockframework/spock Length of output: 1482 🏁 Script executed: #!/bin/bash
# Check if there's any base test class or configuration that SpringRunnerTest might inherit from
rg "class SpringRunnerTest|extends|@Configuration" spock-spring/spring7-test/src/test/groovy/org/spockframework/spring7/SpringRunnerTest.java -A 5 -B 5Repository: spockframework/spock Length of output: 367 Specify context configuration explicitly. The
This prevents reliance on Spring's implicit behavior which may vary across versions or configurations. 🤖 Prompt for AI Agents |
||
|
|
||
| @Test | ||
| public void testThatSpringRunnerExecutesCorrectly() { | ||
| Assert.assertTrue(true); | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package org.spockframework.spring7 | ||
|
|
||
| import groovy.transform.CompileStatic | ||
| import org.springframework.context.annotation.Bean | ||
| import org.springframework.context.annotation.Configuration | ||
| import spock.mock.DetachedMockFactory | ||
|
|
||
| import java.util.concurrent.Executor | ||
|
|
||
| @CompileStatic | ||
| @Configuration | ||
| class TestConfig { | ||
| @Bean | ||
| Executor executor() { | ||
| new DetachedMockFactory().Stub(Executor) | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you use Junit4 here?
Is it due to the
SpringRunnerTest?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have no idea. -:-D
I just copied the
spring6tospring7and theboot3toboot4, changed the packages and made the adjustments to make them run.So everything you said in both PRs is probably valid (didn't look in detail yet) but also for the existing projects.