Skip to content

Commit

Permalink
Add Spring 5 Test Project and update Spring Boot (#799)
Browse files Browse the repository at this point in the history
  • Loading branch information
leonard84 authored Dec 23, 2017
1 parent 8c851c6 commit c60373c
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 10 deletions.
5 changes: 5 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
8 changes: 2 additions & 6 deletions spock-spring/boot-test/boot-test.gradle
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
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}"
}
}


apply plugin: 'spring-boot'
apply plugin: 'org.springframework.boot'



Expand All @@ -31,7 +29,5 @@ dependencies {

repositories {
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}

Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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[]
12 changes: 12 additions & 0 deletions spock-spring/spring5-test/spring5-test.gradle
Original file line number Diff line number Diff line change
@@ -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 }

}

Original file line number Diff line number Diff line change
@@ -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()
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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'
}
}
}
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit c60373c

Please sign in to comment.