1. Introduction
1.绪论
In this short tutorial, we’ll show the benefits of combining the supporting power of Spring Boot‘s testing framework and the expressiveness of the Spock framework whether that be for unit or integration tests.
在这个简短的教程中,我们将展示结合Spring Boot的测试框架的支持能力和Spock框架的表达性的好处,无论是单元测试还是集成测试。
2. Project Setup
2.项目设置
Let’s start with a simple web application. It can greet, change the greeting and reset it back to the default by simple REST calls. Aside from the main class, we use a simple RestController to provide the functionality:
让我们从一个简单的网络应用开始。它可以通过简单的REST调用来问候、改变问候语并将其重置为默认值。 除了主类之外,我们使用一个简单的RestController来提供功能。
@RestController
@RequestMapping("/hello")
public class WebController {
@GetMapping
public String salutation() {
return "Hello world!";
}
}
So the controller greets with ‘Hello world!’. The @RestController annotation and the shortcut annotations ensure the REST endpoint registration.
所以控制器用’Hello world!’来打招呼。@RestController注解和快捷方式注解确保了REST端点的注册。
3. Maven Dependencies for Spock and Spring Boot Test
3.Maven Spock和Spring Boot测试的依赖性
We start by adding the Maven dependencies and if needed Maven plugin configuration.
我们首先添加Maven依赖项,如果需要的话,添加Maven插件配置。
3.1. Adding the Spock Framework Dependencies with Spring Support
3.1.在Spring支持下添加Spock框架的依赖关系
For Spock itself and for the Spring support we need two dependencies:
对于Spock本身和<a href=”https://search.maven.org/classic/#search%7Cga%7C1%7C%20(g%3A%22org.spockframework%22%20AND%20a%3A%22spock-spring%22)>Spring支持,我们需要两个依赖。
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<version>1.2-groovy-2.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-spring</artifactId>
<version>1.2-groovy-2.4</version>
<scope>test</scope>
</dependency>
Notice, that the versions are specified with are a reference to the used groovy version.
注意,指定的版本是对所使用的groovy版本的引用。
3.2. Adding Spring Boot Test
3.2.添加Spring Boot测试
In order to use the testing utilities of Spring Boot Test, we need the following dependency:
为了使用Spring Boot Test的测试工具,我们需要以下依赖性。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.5.0</version>
<scope>test</scope>
</dependency>
3.3. Setting up Groovy
3.3.设置Groovy
And since Spock is based on Groovy, we have to add and configure the gmavenplus-plugin as well to be able to use this language in our tests:
由于Spock是基于Groovy,我们必须同时添加和配置gmavenplus-plugin才能在测试中使用这种语言。
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<goals>
<goal>compileTests</goal>
</goals>
</execution>
</executions>
</plugin>
Note, that since we only need Groovy for test purposes and therefore we restrict the plugin goal to compileTest.
注意,由于我们只需要Groovy用于测试目的,因此我们将插件的目标限制为compileTest。
4. Loading the ApplicationContext in a Spock Test
4.在Spock测试中加载ApplicationContext
One simple test is to check if all Beans in the Spring application context are created:
一个简单的测试是检查Spring应用上下文中的所有Bean是否已经创建。
@SpringBootTest
class LoadContextTest extends Specification {
@Autowired (required = false)
private WebController webController
def "when context is loaded then all expected beans are created"() {
expect: "the WebController is created"
webController
}
}
For this integration test, we need to start up the ApplicationContext, which is what @SpringBootTest does for us. Spock provides the section separation in our test with the keywords like “when”, “then” or “expect”.
对于这个集成测试,我们需要启动ApplicationContext,这就是@SpringBootTest为我们所做的。Spock在我们的测试中用”when”、”then”或”expect”这样的关键字提供了部分分离。
In addition, we can exploit Groovy Truth to check if a bean is null, as the last line of our test here.
此外,我们可以利用Groovy Truth来检查一个bean是否为null,就像我们这里测试的最后一行。
5. Using WebMvcTest in a Spock Test
5.在Spock测试中使用WebMvcTest
Likewise, we can test the behavior of the WebController:
同样地,我们可以测试WebController的行为。
@AutoConfigureMockMvc
@WebMvcTest
class WebControllerTest extends Specification {
@Autowired
private MockMvc mvc
def "when get is performed then the response has status 200 and content is 'Hello world!'"() {
expect: "Status is 200 and the response is 'Hello world!'"
mvc.perform(get("/hello"))
.andExpect(status().isOk())
.andReturn()
.response
.contentAsString == "Hello world!"
}
}
It’s important to note that in our Spock tests (or rather Specifications) we can use all familiar annotations from the Spring Boot test framework that we are used to.
需要注意的是,在我们的Spock测试(或者说Specifications)中我们可以使用我们习惯的Spring Boot测试框架中所有熟悉的注释。
6. Conclusion
6.结论
In this article, we’ve explained how to set up a Maven project to use Spock and the Spring Boot test framework combined. Furthermore, we have seen how both frameworks supplement each other perfectly.
在这篇文章中,我们已经解释了如何设置Maven项目,将Spock和Spring Boot测试框架结合使用。此外,我们还看到这两个框架如何完美地相互补充。
For a deeper dive, have a look to our tutorials about testing with Spring Boot, about the Spock framework and about the Groovy language.
要想深入了解,请看我们关于使用Spring Boot进行测试、关于Spock框架和关于Groovy语言的教程。
Finally, the source code with additional examples can be found in our GitHub repository.
最后,可以在我们的GitHub仓库中找到带有额外示例的源代码。