-
Notifications
You must be signed in to change notification settings - Fork 476
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
base: master
Are you sure you want to change the base?
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.0" | ||||||
|
|
||||||
| 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 | ||||||
|
Member
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. Why do you use Junit4 here?
Member
Author
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. I have no idea. -:-D |
||||||
| testImplementation "org.springframework:spring-context" | ||||||
| testImplementation("org.springframework:spring-test") | ||||||
|
Member
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. To make is same as the other dependencies.
Suggested change
|
||||||
|
|
||||||
| } | ||||||
|
|
||||||
|
|
||||||
| 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 { | ||
|
|
||
| @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) | ||
| } | ||
| } |
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.
Wouldn't it be good to move the version into the libs.versions.toml, and use it there in a dependency, just do make it updateable via renovate, something like
spring7Versionsimilar to junit?