diff --git a/settings.gradle b/settings.gradle index d1b176cb47..bac85796b8 100755 --- a/settings.gradle +++ b/settings.gradle @@ -14,6 +14,11 @@ if (JavaVersion.current().java7Compatible) { include "spock-spring:boot-test" } +if (JavaVersion.current().java8Compatible) { + include "spock-spring:spring5-test" +} + + rootProject.name = "spock" nameBuildScriptsAfterProjectNames(rootProject.children) diff --git a/spock-spring/boot-test/boot-test.gradle b/spock-spring/boot-test/boot-test.gradle index b8921b06b6..cbd80ea364 100644 --- a/spock-spring/boot-test/boot-test.gradle +++ b/spock-spring/boot-test/boot-test.gradle @@ -1,11 +1,9 @@ buildscript { ext { - springBootVersion = '1.4.0.RELEASE' + springBootVersion = '1.5.9.RELEASE' } repositories { mavenCentral() - maven { url "https://repo.spring.io/snapshot" } - maven { url "https://repo.spring.io/milestone" } } dependencies { classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}" @@ -13,7 +11,7 @@ buildscript { } -apply plugin: 'spring-boot' +apply plugin: 'org.springframework.boot' @@ -31,7 +29,5 @@ dependencies { repositories { mavenCentral() - maven { url "https://repo.spring.io/snapshot" } - maven { url "https://repo.spring.io/milestone" } } diff --git a/spock-spring/boot-test/src/test/groovy/org/spockframework/boot/SimpleBootAppIntegrationSpec.groovy b/spock-spring/boot-test/src/test/groovy/org/spockframework/boot/SimpleBootAppIntegrationSpec.groovy index 26a64499fa..621fe7cceb 100644 --- a/spock-spring/boot-test/src/test/groovy/org/spockframework/boot/SimpleBootAppIntegrationSpec.groovy +++ b/spock-spring/boot-test/src/test/groovy/org/spockframework/boot/SimpleBootAppIntegrationSpec.groovy @@ -17,11 +17,9 @@ package org.spockframework.boot import org.springframework.beans.factory.annotation.Autowired -import org.springframework.boot.test.SpringApplicationConfiguration +import org.springframework.boot.test.context.SpringBootTest import org.springframework.context.ApplicationContext - import spock.lang.Specification - /** * Spock test for {@link org.spockframework.boot.SimpleBootApp}. * Adapted from https://github.com/spring-projects/spring-boot/blob/master/spring-boot-samples/ @@ -30,7 +28,7 @@ import spock.lang.Specification * @author Dave Syer * @author Peter Niederwieser */ -@SpringApplicationConfiguration(classes = SimpleBootApp.class) +@SpringBootTest(classes = SimpleBootApp) class SimpleBootAppIntegrationSpec extends Specification { @Autowired ApplicationContext context diff --git a/spock-spring/boot-test/src/test/groovy/org/spockframework/boot/SpringBeanIntegrationSpec.groovy b/spock-spring/boot-test/src/test/groovy/org/spockframework/boot/SpringBeanIntegrationSpec.groovy new file mode 100644 index 0000000000..3a9a32f7c8 --- /dev/null +++ b/spock-spring/boot-test/src/test/groovy/org/spockframework/boot/SpringBeanIntegrationSpec.groovy @@ -0,0 +1,53 @@ +/* + * Copyright 2012-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.spockframework.boot + +import org.spockframework.boot.service.HelloWorldService +import org.spockframework.spring.SpringBean +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest +import org.springframework.test.web.servlet.MockMvc +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders +import spock.lang.Specification + +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status +/** + * Integration tests for ensuring compatibility with Spring-Boot's {@link WebMvcTest} annotation + * in conjunction with {@link SpringBean}. + */ +//tag::include[] +@WebMvcTest +class SpringBeanIntegrationSpec extends Specification { + + @Autowired + MockMvc mvc + + @SpringBean + HelloWorldService helloWorldService = Stub() + + def "spring context loads for web mvc slice"() { + given: + helloWorldService.getHelloMessage() >> 'hello world' + + expect: "controller is available" + mvc.perform(MockMvcRequestBuilders.get("/")) + .andExpect(status().isOk()) + .andExpect(content().string("hello world")) + } +} +//end::include[] diff --git a/spock-spring/spring5-test/spring5-test.gradle b/spock-spring/spring5-test/spring5-test.gradle new file mode 100644 index 0000000000..2eddc8323f --- /dev/null +++ b/spock-spring/spring5-test/spring5-test.gradle @@ -0,0 +1,12 @@ +def springVersion = "5.0.2.RELEASE" + +dependencies { + compile "org.springframework:spring-core:$springVersion" + + testCompile project(":spock-core") + testCompile project(":spock-spring") + testCompile "org.springframework:spring-context:$springVersion" + testCompile ("org.springframework:spring-test:$springVersion") { force = true } + +} + diff --git a/spock-spring/spring5-test/src/test/groovy/org/spockframework/spring5/NoMockConfig.groovy b/spock-spring/spring5-test/src/test/groovy/org/spockframework/spring5/NoMockConfig.groovy new file mode 100644 index 0000000000..2b3dfe396c --- /dev/null +++ b/spock-spring/spring5-test/src/test/groovy/org/spockframework/spring5/NoMockConfig.groovy @@ -0,0 +1,35 @@ +package org.spockframework.spring5 + +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() + } +} diff --git a/spock-spring/spring5-test/src/test/groovy/org/spockframework/spring5/RuntimeCompatibilitySpec.groovy b/spock-spring/spring5-test/src/test/groovy/org/spockframework/spring5/RuntimeCompatibilitySpec.groovy new file mode 100644 index 0000000000..d641f4bccd --- /dev/null +++ b/spock-spring/spring5-test/src/test/groovy/org/spockframework/spring5/RuntimeCompatibilitySpec.groovy @@ -0,0 +1,25 @@ +package org.spockframework.spring5 + +import spock.lang.Specification + +import java.util.concurrent.Executor + +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.test.context.ContextConfiguration + +@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 + } +} diff --git a/spock-spring/spring5-test/src/test/groovy/org/spockframework/spring5/SpringBeanTest.groovy b/spock-spring/spring5-test/src/test/groovy/org/spockframework/spring5/SpringBeanTest.groovy new file mode 100644 index 0000000000..f20470ef17 --- /dev/null +++ b/spock-spring/spring5-test/src/test/groovy/org/spockframework/spring5/SpringBeanTest.groovy @@ -0,0 +1,30 @@ +package org.spockframework.spring5 + +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' + } + } +} diff --git a/spock-spring/spring5-test/src/test/groovy/org/spockframework/spring5/TestConfig.groovy b/spock-spring/spring5-test/src/test/groovy/org/spockframework/spring5/TestConfig.groovy new file mode 100644 index 0000000000..c888fbd3bc --- /dev/null +++ b/spock-spring/spring5-test/src/test/groovy/org/spockframework/spring5/TestConfig.groovy @@ -0,0 +1,18 @@ +package org.spockframework.spring5 + +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) + } +} +