From b111a557d65efe3b6909b50e427552339c372b9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leonard=20Br=C3=BCnings?= Date: Thu, 28 Jun 2018 19:24:35 +0200 Subject: [PATCH] Add Spring Boot 2 Test Project --- settings.gradle | 1 + .../spockframework/boot/SimpleBootApp.java | 7 +- .../org/spockframework/boot/jpa/Book.java | 8 +- .../boot/web/HelloWorldController.java | 20 ++++- .../boot/DataJpaTestIntegrationSpec.groovy | 14 ++-- .../boot/SimpleBootAppIntegrationSpec.groovy | 8 +- .../boot/SpringBeanIntegrationSpec.groovy | 8 +- ...ngBootTestAnnotationIntegrationSpec.groovy | 7 +- ...ingBootTestAnnotationScopedMockSpec.groovy | 12 +-- ...otTestAnnotationScopedProxyMockSpec.groovy | 15 ++-- .../boot/WebMvcTestIntegrationSpec.groovy | 9 ++- spock-spring/boot2-test/boot2-test.gradle | 37 +++++++++ .../spockframework/boot2/SimpleBootApp.java | 33 ++++++++ .../org/spockframework/boot2/jpa/Book.java | 40 ++++++++++ .../boot2/jpa/BookRepository.java | 26 +++++++ .../boot2/service/HelloWorldService.java | 32 ++++++++ .../service/ScopedHelloWorldService.java | 19 +++++ .../boot2/web/HelloWorldController.java | 26 +++++++ .../src/main/resources/application.properties | 3 + .../boot2/DataJpaTestIntegrationSpec.groovy | 48 ++++++++++++ .../boot2/SimpleBootAppIntegrationSpec.groovy | 45 +++++++++++ .../boot2/SpringBeanIntegrationSpec.groovy | 55 ++++++++++++++ ...ngBootTestAnnotationIntegrationSpec.groovy | 43 +++++++++++ ...ingBootTestAnnotationScopedMockSpec.groovy | 71 ++++++++++++++++++ ...otTestAnnotationScopedProxyMockSpec.groovy | 75 +++++++++++++++++++ .../boot2/WebMvcTestIntegrationSpec.groovy | 66 ++++++++++++++++ 26 files changed, 682 insertions(+), 46 deletions(-) create mode 100644 spock-spring/boot2-test/boot2-test.gradle create mode 100644 spock-spring/boot2-test/src/main/java/org/spockframework/boot2/SimpleBootApp.java create mode 100644 spock-spring/boot2-test/src/main/java/org/spockframework/boot2/jpa/Book.java create mode 100644 spock-spring/boot2-test/src/main/java/org/spockframework/boot2/jpa/BookRepository.java create mode 100644 spock-spring/boot2-test/src/main/java/org/spockframework/boot2/service/HelloWorldService.java create mode 100644 spock-spring/boot2-test/src/main/java/org/spockframework/boot2/service/ScopedHelloWorldService.java create mode 100644 spock-spring/boot2-test/src/main/java/org/spockframework/boot2/web/HelloWorldController.java create mode 100644 spock-spring/boot2-test/src/main/resources/application.properties create mode 100644 spock-spring/boot2-test/src/test/groovy/org/spockframework/boot2/DataJpaTestIntegrationSpec.groovy create mode 100644 spock-spring/boot2-test/src/test/groovy/org/spockframework/boot2/SimpleBootAppIntegrationSpec.groovy create mode 100644 spock-spring/boot2-test/src/test/groovy/org/spockframework/boot2/SpringBeanIntegrationSpec.groovy create mode 100644 spock-spring/boot2-test/src/test/groovy/org/spockframework/boot2/SpringBootTestAnnotationIntegrationSpec.groovy create mode 100644 spock-spring/boot2-test/src/test/groovy/org/spockframework/boot2/SpringBootTestAnnotationScopedMockSpec.groovy create mode 100644 spock-spring/boot2-test/src/test/groovy/org/spockframework/boot2/SpringBootTestAnnotationScopedProxyMockSpec.groovy create mode 100644 spock-spring/boot2-test/src/test/groovy/org/spockframework/boot2/WebMvcTestIntegrationSpec.groovy diff --git a/settings.gradle b/settings.gradle index bac85796b8..9ffa29b756 100755 --- a/settings.gradle +++ b/settings.gradle @@ -15,6 +15,7 @@ if (JavaVersion.current().java7Compatible) { } if (JavaVersion.current().java8Compatible) { + include "spock-spring:boot2-test" include "spock-spring:spring5-test" } diff --git a/spock-spring/boot-test/src/main/java/org/spockframework/boot/SimpleBootApp.java b/spock-spring/boot-test/src/main/java/org/spockframework/boot/SimpleBootApp.java index 9e38eb945f..95acb2168c 100644 --- a/spock-spring/boot-test/src/main/java/org/spockframework/boot/SimpleBootApp.java +++ b/spock-spring/boot-test/src/main/java/org/spockframework/boot/SimpleBootApp.java @@ -1,11 +1,11 @@ /* - * Copyright 2012-2013 the original author or authors. + * Copyright 2018 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 + * 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, @@ -16,8 +16,7 @@ package org.spockframework.boot; -import org.springframework.boot.CommandLineRunner; -import org.springframework.boot.SpringApplication; +import org.springframework.boot.*; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication diff --git a/spock-spring/boot-test/src/main/java/org/spockframework/boot/jpa/Book.java b/spock-spring/boot-test/src/main/java/org/spockframework/boot/jpa/Book.java index 3fda14e380..885d17b460 100644 --- a/spock-spring/boot-test/src/main/java/org/spockframework/boot/jpa/Book.java +++ b/spock-spring/boot-test/src/main/java/org/spockframework/boot/jpa/Book.java @@ -1,11 +1,11 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2018 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 + * 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, @@ -17,9 +17,7 @@ package org.spockframework.boot.jpa; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.Id; +import javax.persistence.*; /** * JPA example entity class. diff --git a/spock-spring/boot-test/src/main/java/org/spockframework/boot/web/HelloWorldController.java b/spock-spring/boot-test/src/main/java/org/spockframework/boot/web/HelloWorldController.java index 385683487c..e61b4771ed 100644 --- a/spock-spring/boot-test/src/main/java/org/spockframework/boot/web/HelloWorldController.java +++ b/spock-spring/boot-test/src/main/java/org/spockframework/boot/web/HelloWorldController.java @@ -1,9 +1,25 @@ +/* + * Copyright 2018 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.web; import org.spockframework.boot.service.HelloWorldService; + import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; /** * Spring-MVC controller class. diff --git a/spock-spring/boot-test/src/test/groovy/org/spockframework/boot/DataJpaTestIntegrationSpec.groovy b/spock-spring/boot-test/src/test/groovy/org/spockframework/boot/DataJpaTestIntegrationSpec.groovy index dc895502c7..21603fcb07 100644 --- a/spock-spring/boot-test/src/test/groovy/org/spockframework/boot/DataJpaTestIntegrationSpec.groovy +++ b/spock-spring/boot-test/src/test/groovy/org/spockframework/boot/DataJpaTestIntegrationSpec.groovy @@ -1,11 +1,11 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2018 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 + * 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, @@ -17,14 +17,12 @@ package org.spockframework.boot -import org.spockframework.boot.jpa.Book -import org.spockframework.boot.jpa.BookRepository -import org.springframework.beans.factory.annotation.Autowired -import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest -import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager -import org.springframework.test.context.ContextConfiguration +import org.spockframework.boot.jpa.* import spock.lang.Specification +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.autoconfigure.orm.jpa.* + /** * Integration tests for ensuring compatibility with Spring-Boot's {@link DataJpaTest} annotation. */ 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 621fe7cceb..aa2e2d4bd8 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 @@ -1,11 +1,11 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2018 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 + * 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, @@ -16,10 +16,12 @@ package org.spockframework.boot +import spock.lang.Specification + import org.springframework.beans.factory.annotation.Autowired 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/ 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 index 3a9a32f7c8..ceeacb2fb5 100644 --- 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 @@ -1,11 +1,11 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2018 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 + * 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, @@ -18,14 +18,16 @@ package org.spockframework.boot import org.spockframework.boot.service.HelloWorldService import org.spockframework.spring.SpringBean +import spock.lang.Specification + 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}. diff --git a/spock-spring/boot-test/src/test/groovy/org/spockframework/boot/SpringBootTestAnnotationIntegrationSpec.groovy b/spock-spring/boot-test/src/test/groovy/org/spockframework/boot/SpringBootTestAnnotationIntegrationSpec.groovy index d18f562c54..b206d14f61 100644 --- a/spock-spring/boot-test/src/test/groovy/org/spockframework/boot/SpringBootTestAnnotationIntegrationSpec.groovy +++ b/spock-spring/boot-test/src/test/groovy/org/spockframework/boot/SpringBootTestAnnotationIntegrationSpec.groovy @@ -1,11 +1,11 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2018 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 + * 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, @@ -16,10 +16,11 @@ package org.spockframework.boot +import spock.lang.Specification + import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.context.SpringBootTest import org.springframework.context.ApplicationContext -import spock.lang.Specification /** * Integration test similar to {@link SimpleBootAppIntegrationSpec} but using the {@link SpringBootTest} annotation. diff --git a/spock-spring/boot-test/src/test/groovy/org/spockframework/boot/SpringBootTestAnnotationScopedMockSpec.groovy b/spock-spring/boot-test/src/test/groovy/org/spockframework/boot/SpringBootTestAnnotationScopedMockSpec.groovy index a4ae6088d1..1550a571b6 100644 --- a/spock-spring/boot-test/src/test/groovy/org/spockframework/boot/SpringBootTestAnnotationScopedMockSpec.groovy +++ b/spock-spring/boot-test/src/test/groovy/org/spockframework/boot/SpringBootTestAnnotationScopedMockSpec.groovy @@ -1,11 +1,11 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2018 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 + * 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, @@ -18,13 +18,13 @@ package org.spockframework.boot import org.spockframework.boot.service.HelloWorldService import org.spockframework.spring.ScanScopedBeans +import spock.lang.Specification +import spock.mock.DetachedMockFactory + import org.springframework.beans.factory.annotation.Autowired -import org.springframework.boot.test.context.SpringBootTest -import org.springframework.boot.test.context.TestConfiguration +import org.springframework.boot.test.context.* import org.springframework.context.ApplicationContext import org.springframework.context.annotation.Bean -import spock.lang.Specification -import spock.mock.DetachedMockFactory /** * Tests enabled scanning of scoped beans. diff --git a/spock-spring/boot-test/src/test/groovy/org/spockframework/boot/SpringBootTestAnnotationScopedProxyMockSpec.groovy b/spock-spring/boot-test/src/test/groovy/org/spockframework/boot/SpringBootTestAnnotationScopedProxyMockSpec.groovy index 7227f10d87..33824b1672 100644 --- a/spock-spring/boot-test/src/test/groovy/org/spockframework/boot/SpringBootTestAnnotationScopedProxyMockSpec.groovy +++ b/spock-spring/boot-test/src/test/groovy/org/spockframework/boot/SpringBootTestAnnotationScopedProxyMockSpec.groovy @@ -1,11 +1,11 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2018 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 + * 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, @@ -18,15 +18,14 @@ package org.spockframework.boot import org.spockframework.boot.service.HelloWorldService import org.spockframework.spring.ScanScopedBeans +import spock.lang.Specification +import spock.mock.DetachedMockFactory + import org.springframework.beans.factory.annotation.Autowired -import org.springframework.boot.test.context.SpringBootTest -import org.springframework.boot.test.context.TestConfiguration +import org.springframework.boot.test.context.* import org.springframework.context.ApplicationContext -import org.springframework.context.annotation.Bean -import org.springframework.context.annotation.ScopedProxyMode +import org.springframework.context.annotation.* import org.springframework.web.context.annotation.RequestScope -import spock.lang.Specification -import spock.mock.DetachedMockFactory /** * Tests enabled scanning of scoped and proxied beans. diff --git a/spock-spring/boot-test/src/test/groovy/org/spockframework/boot/WebMvcTestIntegrationSpec.groovy b/spock-spring/boot-test/src/test/groovy/org/spockframework/boot/WebMvcTestIntegrationSpec.groovy index 6aaf92e175..336f23ed00 100644 --- a/spock-spring/boot-test/src/test/groovy/org/spockframework/boot/WebMvcTestIntegrationSpec.groovy +++ b/spock-spring/boot-test/src/test/groovy/org/spockframework/boot/WebMvcTestIntegrationSpec.groovy @@ -1,11 +1,11 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2018 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 + * 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, @@ -17,14 +17,15 @@ package org.spockframework.boot import org.spockframework.boot.service.HelloWorldService +import spock.lang.Specification +import spock.mock.DetachedMockFactory + import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest import org.springframework.boot.test.context.TestConfiguration import org.springframework.context.annotation.Bean import org.springframework.test.web.servlet.MockMvc import org.springframework.test.web.servlet.request.MockMvcRequestBuilders -import spock.lang.Specification -import spock.mock.DetachedMockFactory import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status diff --git a/spock-spring/boot2-test/boot2-test.gradle b/spock-spring/boot2-test/boot2-test.gradle new file mode 100644 index 0000000000..10151c6c28 --- /dev/null +++ b/spock-spring/boot2-test/boot2-test.gradle @@ -0,0 +1,37 @@ +/* + * Copyright 2018 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. + */ + +plugins { + id "io.spring.dependency-management" version "1.0.5.RELEASE" + id 'org.springframework.boot' version "2.0.3.RELEASE" +} + +dependencies { + compile "org.springframework.boot:spring-boot-starter-data-jpa" + compile "org.springframework.boot:spring-boot-starter-web" + + testCompile "org.springframework.boot:spring-boot-starter-test" + testCompile project(":spock-core") + testCompile project(":spock-spring") + + runtime "com.h2database:h2" + +} + +repositories { + mavenCentral() +} + diff --git a/spock-spring/boot2-test/src/main/java/org/spockframework/boot2/SimpleBootApp.java b/spock-spring/boot2-test/src/main/java/org/spockframework/boot2/SimpleBootApp.java new file mode 100644 index 0000000000..7980478fc0 --- /dev/null +++ b/spock-spring/boot2-test/src/main/java/org/spockframework/boot2/SimpleBootApp.java @@ -0,0 +1,33 @@ +/* + * Copyright 2012-2013 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.boot2; + +import org.springframework.boot.*; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SimpleBootApp implements CommandLineRunner { + + @Override + public void run(String... args) { + System.out.println("Hello World"); + } + + public static void main(String[] args) throws Exception { + SpringApplication.run(SimpleBootApp.class, args); + } +} diff --git a/spock-spring/boot2-test/src/main/java/org/spockframework/boot2/jpa/Book.java b/spock-spring/boot2-test/src/main/java/org/spockframework/boot2/jpa/Book.java new file mode 100644 index 0000000000..1950027a3b --- /dev/null +++ b/spock-spring/boot2-test/src/main/java/org/spockframework/boot2/jpa/Book.java @@ -0,0 +1,40 @@ +/* + * 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.boot2.jpa; + +import javax.persistence.*; + +/** + * JPA example entity class. + */ +@Entity +public class Book { + + @Id + @GeneratedValue + private Long id; + private String title; + + protected Book() { + // no-args JPA constructor + } + + public Book(String title) { + this.title = title; + } +} diff --git a/spock-spring/boot2-test/src/main/java/org/spockframework/boot2/jpa/BookRepository.java b/spock-spring/boot2-test/src/main/java/org/spockframework/boot2/jpa/BookRepository.java new file mode 100644 index 0000000000..b0c8d101db --- /dev/null +++ b/spock-spring/boot2-test/src/main/java/org/spockframework/boot2/jpa/BookRepository.java @@ -0,0 +1,26 @@ +/* + * 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.boot2.jpa; + +import org.springframework.data.repository.CrudRepository; + +/** + * Spring-Data repository for {@link Book} entities. + */ +public interface BookRepository extends CrudRepository { +} diff --git a/spock-spring/boot2-test/src/main/java/org/spockframework/boot2/service/HelloWorldService.java b/spock-spring/boot2-test/src/main/java/org/spockframework/boot2/service/HelloWorldService.java new file mode 100644 index 0000000000..81e768d7bc --- /dev/null +++ b/spock-spring/boot2-test/src/main/java/org/spockframework/boot2/service/HelloWorldService.java @@ -0,0 +1,32 @@ +/* + * Copyright 2012-2013 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.boot2.service; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +@Component +public class HelloWorldService { + + @Value("${name:World}") + private String name; + + public String getHelloMessage() { + return "Hello " + this.name; + } + +} diff --git a/spock-spring/boot2-test/src/main/java/org/spockframework/boot2/service/ScopedHelloWorldService.java b/spock-spring/boot2-test/src/main/java/org/spockframework/boot2/service/ScopedHelloWorldService.java new file mode 100644 index 0000000000..cbaa2db3fc --- /dev/null +++ b/spock-spring/boot2-test/src/main/java/org/spockframework/boot2/service/ScopedHelloWorldService.java @@ -0,0 +1,19 @@ +package org.spockframework.boot2.service; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.ScopedProxyMode; +import org.springframework.stereotype.Component; +import org.springframework.web.context.annotation.RequestScope; + +@RequestScope(proxyMode = ScopedProxyMode.TARGET_CLASS) +@Component +public class ScopedHelloWorldService { + + @Value("${name:World Scope!}") + private String name; + + public String getHelloMessage() { + return "Hello " + this.name; + } + +} diff --git a/spock-spring/boot2-test/src/main/java/org/spockframework/boot2/web/HelloWorldController.java b/spock-spring/boot2-test/src/main/java/org/spockframework/boot2/web/HelloWorldController.java new file mode 100644 index 0000000000..8ae792f295 --- /dev/null +++ b/spock-spring/boot2-test/src/main/java/org/spockframework/boot2/web/HelloWorldController.java @@ -0,0 +1,26 @@ +package org.spockframework.boot2.web; + +import org.spockframework.boot2.service.HelloWorldService; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +/** + * Spring-MVC controller class. + */ +@RestController +public class HelloWorldController { + + private HelloWorldService service; + + @Autowired + public HelloWorldController(HelloWorldService service) { + this.service = service; + } + + @RequestMapping("/") + public String hello() { + return service.getHelloMessage(); + } + +} diff --git a/spock-spring/boot2-test/src/main/resources/application.properties b/spock-spring/boot2-test/src/main/resources/application.properties new file mode 100644 index 0000000000..57064eead2 --- /dev/null +++ b/spock-spring/boot2-test/src/main/resources/application.properties @@ -0,0 +1,3 @@ +name= Spock +logging.level.root=OFF +spring.jpa.hibernate.ddl-auto=create diff --git a/spock-spring/boot2-test/src/test/groovy/org/spockframework/boot2/DataJpaTestIntegrationSpec.groovy b/spock-spring/boot2-test/src/test/groovy/org/spockframework/boot2/DataJpaTestIntegrationSpec.groovy new file mode 100644 index 0000000000..89654f1218 --- /dev/null +++ b/spock-spring/boot2-test/src/test/groovy/org/spockframework/boot2/DataJpaTestIntegrationSpec.groovy @@ -0,0 +1,48 @@ +/* + * 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.boot2 + +import org.spockframework.boot2.jpa.* +import spock.lang.Specification + +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.autoconfigure.orm.jpa.* + +/** + * Integration tests for ensuring compatibility with Spring-Boot's {@link DataJpaTest} annotation. + */ +@DataJpaTest +class DataJpaTestIntegrationSpec extends Specification { + + @Autowired + TestEntityManager entityManager + + @Autowired + BookRepository bookRepository + + def "spring context loads for data jpa slice"() { + given: "some existing books" + entityManager.persist(new Book("Moby Dick")) + entityManager.persist(new Book("Testing Spring with Spock")) + + expect: "the correct count is inside the repository" + bookRepository.count() == 2L + } + + +} diff --git a/spock-spring/boot2-test/src/test/groovy/org/spockframework/boot2/SimpleBootAppIntegrationSpec.groovy b/spock-spring/boot2-test/src/test/groovy/org/spockframework/boot2/SimpleBootAppIntegrationSpec.groovy new file mode 100644 index 0000000000..65145ba237 --- /dev/null +++ b/spock-spring/boot2-test/src/test/groovy/org/spockframework/boot2/SimpleBootAppIntegrationSpec.groovy @@ -0,0 +1,45 @@ +/* + * Copyright 2012-2014 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.boot2 + +import spock.lang.Specification + +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.context.ApplicationContext + +/** + * Spock test for {@link org.spockframework.boot2.SimpleBootApp}. + * Adapted from https://github.com/spring-projects/spring-boot/blob/master/spring-boot-samples/ + * spring-boot2-sample-simple/src/test/java/sample/simple/SpringTestSampleSimpleApplicationTests.java. + * + * @author Dave Syer + * @author Peter Niederwieser + */ +@SpringBootTest(classes = SimpleBootApp) +class SimpleBootAppIntegrationSpec extends Specification { + @Autowired + ApplicationContext context + + def "test context loads"() { + expect: + context != null + context.containsBean("helloWorldService") + context.containsBean("simpleBootApp") + context.containsBean("scopedHelloWorldService") + } +} diff --git a/spock-spring/boot2-test/src/test/groovy/org/spockframework/boot2/SpringBeanIntegrationSpec.groovy b/spock-spring/boot2-test/src/test/groovy/org/spockframework/boot2/SpringBeanIntegrationSpec.groovy new file mode 100644 index 0000000000..3f49a65add --- /dev/null +++ b/spock-spring/boot2-test/src/test/groovy/org/spockframework/boot2/SpringBeanIntegrationSpec.groovy @@ -0,0 +1,55 @@ +/* + * 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.boot2 + +import org.spockframework.boot2.service.HelloWorldService +import org.spockframework.spring.SpringBean +import spock.lang.Specification + +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 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/boot2-test/src/test/groovy/org/spockframework/boot2/SpringBootTestAnnotationIntegrationSpec.groovy b/spock-spring/boot2-test/src/test/groovy/org/spockframework/boot2/SpringBootTestAnnotationIntegrationSpec.groovy new file mode 100644 index 0000000000..ed47664b17 --- /dev/null +++ b/spock-spring/boot2-test/src/test/groovy/org/spockframework/boot2/SpringBootTestAnnotationIntegrationSpec.groovy @@ -0,0 +1,43 @@ +/* + * Copyright 2012-2014 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.boot2 + +import spock.lang.Specification + +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.context.ApplicationContext + +/** + * Integration test similar to {@link SimpleBootAppIntegrationSpec} but using the {@link SpringBootTest} annotation. + * + * SpringBootTest.webEnvironment needs to something other than {@link SpringBootTest.WebEnvironment.MOCK}, + * otherwise there is always a MockRequest active. + */ +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE) +class SpringBootTestAnnotationIntegrationSpec extends Specification { + @Autowired + ApplicationContext context + + def "test context loads"() { + expect: + context != null + context.containsBean("helloWorldService") + context.containsBean("simpleBootApp") + context.containsBean("scopedHelloWorldService") + } +} diff --git a/spock-spring/boot2-test/src/test/groovy/org/spockframework/boot2/SpringBootTestAnnotationScopedMockSpec.groovy b/spock-spring/boot2-test/src/test/groovy/org/spockframework/boot2/SpringBootTestAnnotationScopedMockSpec.groovy new file mode 100644 index 0000000000..a8008f3004 --- /dev/null +++ b/spock-spring/boot2-test/src/test/groovy/org/spockframework/boot2/SpringBootTestAnnotationScopedMockSpec.groovy @@ -0,0 +1,71 @@ +/* + * Copyright 2012-2014 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.boot2 + +import org.spockframework.boot2.service.HelloWorldService +import org.spockframework.spring.ScanScopedBeans +import spock.lang.Specification +import spock.mock.DetachedMockFactory + +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.context.* +import org.springframework.context.ApplicationContext +import org.springframework.context.annotation.Bean + +/** + * Tests enabled scanning of scoped beans. + */ +@ScanScopedBeans +@SpringBootTest +class SpringBootTestAnnotationScopedMockSpec extends Specification { + @Autowired + ApplicationContext context + + @Autowired + HelloWorldService helloWorldService + + def "test context loads"() { + expect: + context != null + context.containsBean("helloWorldService") + !context.containsBean("scopedTarget.helloWorldService") + context.containsBean("simpleBootApp") + } + + def "scoped mock can be used"() { + expect: + !helloWorldService.class.simpleName.startsWith('HelloWorldService$$EnhancerBySpringCGLIB$$') + + when: + def result = helloWorldService.helloMessage + + then: + 1 * helloWorldService.getHelloMessage() >> 'sup?' + result == 'sup?' + } + + + @TestConfiguration + static class MockConfig { + def detachedMockFactory = new DetachedMockFactory() + + @Bean + HelloWorldService helloWorldService() { + return detachedMockFactory.Mock(HelloWorldService) + } + } +} diff --git a/spock-spring/boot2-test/src/test/groovy/org/spockframework/boot2/SpringBootTestAnnotationScopedProxyMockSpec.groovy b/spock-spring/boot2-test/src/test/groovy/org/spockframework/boot2/SpringBootTestAnnotationScopedProxyMockSpec.groovy new file mode 100644 index 0000000000..b3ef92ddb2 --- /dev/null +++ b/spock-spring/boot2-test/src/test/groovy/org/spockframework/boot2/SpringBootTestAnnotationScopedProxyMockSpec.groovy @@ -0,0 +1,75 @@ +/* + * Copyright 2012-2014 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.boot2 + +import org.spockframework.boot2.service.HelloWorldService +import org.spockframework.spring.ScanScopedBeans +import spock.lang.Specification +import spock.mock.DetachedMockFactory + +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.context.* +import org.springframework.context.ApplicationContext +import org.springframework.context.annotation.* +import org.springframework.web.context.annotation.RequestScope + +/** + * Tests enabled scanning of scoped and proxied beans. + */ +@ScanScopedBeans +@SpringBootTest +class SpringBootTestAnnotationScopedProxyMockSpec extends Specification { + @Autowired + ApplicationContext context + + @Autowired + HelloWorldService helloWorldService + + def "test context loads"() { + expect: + context != null + context.containsBean("helloWorldService") + context.containsBean("scopedTarget.helloWorldService") + context.containsBean("simpleBootApp") + } + + def "scoped mock can be used"() { + given: + def helloWorldServiceMock = context.getBean("scopedTarget.helloWorldService") as HelloWorldService + + expect: + helloWorldService.class.simpleName.startsWith('HelloWorldService$$EnhancerBySpringCGLIB$$') + + when: + def result = helloWorldService.helloMessage + + then: + 1 * helloWorldServiceMock.getHelloMessage() >> 'sup?' + result == 'sup?' + } + + + @TestConfiguration + static class MockConfig { + def detachedMockFactory = new DetachedMockFactory() + + @Bean @RequestScope(proxyMode = ScopedProxyMode.TARGET_CLASS) + HelloWorldService helloWorldService() { + return detachedMockFactory.Mock(HelloWorldService) + } + } +} diff --git a/spock-spring/boot2-test/src/test/groovy/org/spockframework/boot2/WebMvcTestIntegrationSpec.groovy b/spock-spring/boot2-test/src/test/groovy/org/spockframework/boot2/WebMvcTestIntegrationSpec.groovy new file mode 100644 index 0000000000..10139fdbcc --- /dev/null +++ b/spock-spring/boot2-test/src/test/groovy/org/spockframework/boot2/WebMvcTestIntegrationSpec.groovy @@ -0,0 +1,66 @@ +/* + * 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.boot2 + +import org.spockframework.boot2.service.HelloWorldService +import spock.lang.Specification +import spock.mock.DetachedMockFactory + +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest +import org.springframework.boot.test.context.TestConfiguration +import org.springframework.context.annotation.Bean +import org.springframework.test.web.servlet.MockMvc +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders + +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. + */ +//tag::include[] +@WebMvcTest +class WebMvcTestIntegrationSpec extends Specification { + + @Autowired + MockMvc mvc + + @Autowired + HelloWorldService helloWorldService + + 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")) + } + + @TestConfiguration + static class MockConfig { + def detachedMockFactory = new DetachedMockFactory() + + @Bean + HelloWorldService helloWorldService() { + return detachedMockFactory.Stub(HelloWorldService) + } + } +} +//end::include[]