-
Notifications
You must be signed in to change notification settings - Fork 480
Add boot4 tests #2261
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
Open
Vampire
wants to merge
1
commit into
master
Choose a base branch
from
boot4-test
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add boot4 tests #2261
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 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 | ||
| * | ||
| * https://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 'org.springframework.boot' version '4.0.3' | ||
| id 'io.spring.dependency-management' version '1.1.7' | ||
| id 'groovy' | ||
| } | ||
|
|
||
| // Spring dependency management downgrades the jupiter version to 5.7.2 otherwise | ||
| ext['junit-jupiter.version'] = libs.versions.junit5.get() | ||
|
|
||
| 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.boot:spring-boot-starter" | ||
| implementation "org.springframework.boot:spring-boot-starter-web" | ||
| implementation "org.springframework.boot:spring-boot-starter-data-jpa" | ||
| testImplementation "org.springframework.boot:spring-boot-starter-test" | ||
| testImplementation "org.springframework.boot:spring-boot-starter-data-jpa-test" | ||
| testImplementation "org.springframework.boot:spring-boot-starter-webmvc-test" | ||
| testImplementation projects.spockCore | ||
| testImplementation projects.spockSpring | ||
|
|
||
| runtimeOnly "com.h2database:h2" | ||
| } | ||
|
|
||
| tasks.named('test') { | ||
| useJUnitPlatform() | ||
| } |
33 changes: 33 additions & 0 deletions
33
spock-spring/boot4-test/src/main/java/org/spockframework/boot4/SimpleBootApp.java
This file contains hidden or 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,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 | ||
| * | ||
| * https://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.boot4; | ||
|
|
||
| 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); | ||
| } | ||
| } |
40 changes: 40 additions & 0 deletions
40
spock-spring/boot4-test/src/main/java/org/spockframework/boot4/jpa/Book.java
This file contains hidden or 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,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 | ||
| * | ||
| * https://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.boot4.jpa; | ||
|
|
||
| import jakarta.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; | ||
| } | ||
| } |
26 changes: 26 additions & 0 deletions
26
spock-spring/boot4-test/src/main/java/org/spockframework/boot4/jpa/BookRepository.java
This file contains hidden or 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,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 | ||
| * | ||
| * https://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.boot4.jpa; | ||
|
|
||
| import org.springframework.data.repository.CrudRepository; | ||
|
|
||
| /** | ||
| * Spring-Data repository for {@link Book} entities. | ||
| */ | ||
| public interface BookRepository extends CrudRepository<Book, Long> { | ||
| } |
32 changes: 32 additions & 0 deletions
32
...k-spring/boot4-test/src/main/java/org/spockframework/boot4/service/HelloWorldService.java
This file contains hidden or 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,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 | ||
| * | ||
| * https://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.boot4.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; | ||
| } | ||
|
|
||
| } |
19 changes: 19 additions & 0 deletions
19
...ng/boot4-test/src/main/java/org/spockframework/boot4/service/ScopedHelloWorldService.java
This file contains hidden or 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,19 @@ | ||
| package org.spockframework.boot4.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; | ||
| } | ||
|
|
||
| } |
26 changes: 26 additions & 0 deletions
26
spock-spring/boot4-test/src/main/java/org/spockframework/boot4/web/HelloWorldController.java
This file contains hidden or 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,26 @@ | ||
| package org.spockframework.boot4.web; | ||
|
|
||
| import org.spockframework.boot4.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(); | ||
| } | ||
|
|
||
| } |
3 changes: 3 additions & 0 deletions
3
spock-spring/boot4-test/src/main/resources/application.properties
This file contains hidden or 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,3 @@ | ||
| name=Spock | ||
| logging.level.root=OFF | ||
| spring.jpa.hibernate.ddl-auto=create |
49 changes: 49 additions & 0 deletions
49
...ing/boot4-test/src/test/groovy/org/spockframework/boot4/DataJpaTestIntegrationSpec.groovy
This file contains hidden or 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,49 @@ | ||
| /* | ||
| * 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 | ||
| * | ||
| * https://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.boot4 | ||
|
|
||
| import org.spockframework.boot4.jpa.* | ||
| import spock.lang.Specification | ||
|
|
||
| import org.springframework.beans.factory.annotation.Autowired | ||
| import org.springframework.boot.data.jpa.test.autoconfigure.DataJpaTest | ||
| import org.springframework.boot.jpa.test.autoconfigure.TestEntityManager | ||
|
|
||
| /** | ||
| * 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 | ||
| } | ||
|
|
||
|
|
||
| } | ||
45 changes: 45 additions & 0 deletions
45
...g/boot4-test/src/test/groovy/org/spockframework/boot4/SimpleBootAppIntegrationSpec.groovy
This file contains hidden or 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,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 | ||
| * | ||
| * https://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.boot4 | ||
|
|
||
| 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.boot4.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") | ||
| } | ||
| } |
55 changes: 55 additions & 0 deletions
55
...ring/boot4-test/src/test/groovy/org/spockframework/boot4/SpringBeanIntegrationSpec.groovy
This file contains hidden or 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,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 | ||
| * | ||
| * https://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.boot4 | ||
|
|
||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content | ||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status | ||
|
|
||
| import org.spockframework.boot4.service.HelloWorldService | ||
| import org.spockframework.spring.SpringBean | ||
| import spock.lang.Specification | ||
|
|
||
| import org.springframework.beans.factory.annotation.Autowired | ||
| import org.springframework.boot.webmvc.test.autoconfigure.WebMvcTest | ||
| import org.springframework.test.web.servlet.MockMvc | ||
| import org.springframework.test.web.servlet.request.MockMvcRequestBuilders | ||
|
|
||
| /** | ||
| * 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[] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Inconsistent package roots for JPA test imports
DataJpaTestis imported fromorg.springframework.boot.data.jpa.test.autoconfigurewhileTestEntityManageris imported fromorg.springframework.boot.jpa.test.autoconfigure— note the difference:data.jpavsjpa. This inconsistency in the package root is worth verifying against the actual Spring Boot 4 module layout. If both classes ship in the same artifact (spring-boot-starter-data-jpa-test), they are likely in the same or a closely related package tree, and one of these imports may be wrong. The boot3 analogue imported both fromorg.springframework.boot.test.autoconfigure.orm.jpa.*with a single wildcard import, which naturally avoided this kind of mismatch.