Skip to content

Commit 0db0c95

Browse files
committed
Add boot4 tests
1 parent b2e8c39 commit 0db0c95

16 files changed

+639
-0
lines changed

settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ if (variant == 2.5) {
103103
} else {
104104
// require Java 17 which isn't supported by Groovy 2.5
105105
include "spock-spring:boot3-test"
106+
include "spock-spring:boot4-test"
106107
include "spock-spring:spring6-test"
107108
}
108109

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
plugins {
17+
id 'org.springframework.boot' version '4.0.0-RC2'
18+
id 'io.spring.dependency-management' version '1.1.7'
19+
id 'groovy'
20+
}
21+
22+
// Spring dependency management downgrades the jupiter version to 5.7.2 otherwise
23+
ext['junit-jupiter.version'] = libs.versions.junit5.get()
24+
25+
java {
26+
toolchain {
27+
languageVersion = JavaLanguageVersion.of(17)
28+
}
29+
}
30+
31+
tasks.withType(JavaCompile).configureEach {
32+
javaCompiler = javaToolchains.compilerFor {
33+
languageVersion = JavaLanguageVersion.of(17)
34+
}
35+
options.encoding = 'UTF-8'
36+
}
37+
38+
dependencies {
39+
implementation "org.springframework.boot:spring-boot-starter"
40+
implementation "org.springframework.boot:spring-boot-starter-web"
41+
implementation "org.springframework.boot:spring-boot-starter-data-jpa"
42+
testImplementation "org.springframework.boot:spring-boot-starter-test"
43+
testImplementation "org.springframework.boot:spring-boot-starter-data-jpa-test"
44+
testImplementation "org.springframework.boot:spring-boot-starter-webmvc-test"
45+
testImplementation projects.spockCore
46+
testImplementation projects.spockSpring
47+
48+
runtimeOnly "com.h2database:h2"
49+
}
50+
51+
tasks.named('test') {
52+
useJUnitPlatform()
53+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2012-2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.spockframework.boot4;
18+
19+
import org.springframework.boot.*;
20+
import org.springframework.boot.autoconfigure.SpringBootApplication;
21+
22+
@SpringBootApplication
23+
public class SimpleBootApp implements CommandLineRunner {
24+
25+
@Override
26+
public void run(String... args) {
27+
System.out.println("Hello World");
28+
}
29+
30+
public static void main(String[] args) throws Exception {
31+
SpringApplication.run(SimpleBootApp.class, args);
32+
}
33+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2012-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
18+
package org.spockframework.boot4.jpa;
19+
20+
import jakarta.persistence.*;
21+
22+
/**
23+
* JPA example entity class.
24+
*/
25+
@Entity
26+
public class Book {
27+
28+
@Id
29+
@GeneratedValue
30+
private Long id;
31+
private String title;
32+
33+
protected Book() {
34+
// no-args JPA constructor
35+
}
36+
37+
public Book(String title) {
38+
this.title = title;
39+
}
40+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2012-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
18+
package org.spockframework.boot4.jpa;
19+
20+
import org.springframework.data.repository.CrudRepository;
21+
22+
/**
23+
* Spring-Data repository for {@link Book} entities.
24+
*/
25+
public interface BookRepository extends CrudRepository<Book, Long> {
26+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2012-2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.spockframework.boot4.service;
18+
19+
import org.springframework.beans.factory.annotation.Value;
20+
import org.springframework.stereotype.Component;
21+
22+
@Component
23+
public class HelloWorldService {
24+
25+
@Value("${name:World}")
26+
private String name;
27+
28+
public String getHelloMessage() {
29+
return "Hello " + this.name;
30+
}
31+
32+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.spockframework.boot4.service;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.context.annotation.ScopedProxyMode;
5+
import org.springframework.stereotype.Component;
6+
import org.springframework.web.context.annotation.RequestScope;
7+
8+
@RequestScope(proxyMode = ScopedProxyMode.TARGET_CLASS)
9+
@Component
10+
public class ScopedHelloWorldService {
11+
12+
@Value("${name:World Scope!}")
13+
private String name;
14+
15+
public String getHelloMessage() {
16+
return "Hello " + this.name;
17+
}
18+
19+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.spockframework.boot4.web;
2+
3+
import org.spockframework.boot4.service.HelloWorldService;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.web.bind.annotation.*;
7+
8+
/**
9+
* Spring-MVC controller class.
10+
*/
11+
@RestController
12+
public class HelloWorldController {
13+
14+
private HelloWorldService service;
15+
16+
@Autowired
17+
public HelloWorldController(HelloWorldService service) {
18+
this.service = service;
19+
}
20+
21+
@RequestMapping("/")
22+
public String hello() {
23+
return service.getHelloMessage();
24+
}
25+
26+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
name=Spock
2+
logging.level.root=OFF
3+
spring.jpa.hibernate.ddl-auto=create
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2012-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
18+
package org.spockframework.boot4
19+
20+
import org.spockframework.boot4.jpa.*
21+
import spock.lang.Specification
22+
23+
import org.springframework.beans.factory.annotation.Autowired
24+
import org.springframework.boot.data.jpa.test.autoconfigure.DataJpaTest
25+
import org.springframework.boot.jpa.test.autoconfigure.TestEntityManager
26+
27+
/**
28+
* Integration tests for ensuring compatibility with Spring-Boot's {@link DataJpaTest} annotation.
29+
*/
30+
@DataJpaTest
31+
class DataJpaTestIntegrationSpec extends Specification {
32+
33+
@Autowired
34+
TestEntityManager entityManager
35+
36+
@Autowired
37+
BookRepository bookRepository
38+
39+
def "spring context loads for data jpa slice"() {
40+
given: "some existing books"
41+
entityManager.persist(new Book("Moby Dick"))
42+
entityManager.persist(new Book("Testing Spring with Spock"))
43+
44+
expect: "the correct count is inside the repository"
45+
bookRepository.count() == 2L
46+
}
47+
48+
49+
}

0 commit comments

Comments
 (0)