Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ if (variant == 2.5) {
} else {
// require Java 17 which isn't supported by Groovy 2.5
include "spock-spring:boot3-test"
include "spock-spring:boot4-test"
include "spock-spring:spring6-test"
}

Expand Down
53 changes: 53 additions & 0 deletions spock-spring/boot4-test/boot4-test.gradle
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()
}
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);
}
}
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;
}
}
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> {
}
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;
}

}
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;
}

}
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();
}

}
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
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
Comment on lines +24 to +25
Copy link

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

DataJpaTest is imported from org.springframework.boot.data.jpa.test.autoconfigure while TestEntityManager is imported from org.springframework.boot.jpa.test.autoconfigure — note the difference: data.jpa vs jpa. 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 from org.springframework.boot.test.autoconfigure.orm.jpa.* with a single wildcard import, which naturally avoided this kind of mismatch.


/**
* 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
}


}
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")
}
}
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[]
Loading
Loading