forked from spockframework/spock
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Spring 5 Test Project and update Spring Boot (spockframework#799)
- Loading branch information
Showing
9 changed files
with
182 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...spring/boot-test/src/test/groovy/org/spockframework/boot/SpringBeanIntegrationSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
|
||
} | ||
|
35 changes: 35 additions & 0 deletions
35
spock-spring/spring5-test/src/test/groovy/org/spockframework/spring5/NoMockConfig.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...g/spring5-test/src/test/groovy/org/spockframework/spring5/RuntimeCompatibilitySpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
spock-spring/spring5-test/src/test/groovy/org/spockframework/spring5/SpringBeanTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
spock-spring/spring5-test/src/test/groovy/org/spockframework/spring5/TestConfig.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
|