我正在尝试使用Spock/Groovy为我的Spring启动应用程序创建测试。我遵循一个指南,第一个测试是测试是否创建了所有的bean。我自动连接了我的一个控制器类ChannelController
,并期望它被创建。然而,这个测试显示它实际上是空的。我在测试文件中创建了一些其他的简单测试(1 + 1 == 2),它们都运行并通过了。有人能帮我吗?
下面是我的pom.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>magic_eight_ball</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>magic_eight_ball</name>
<description>REST Api for Magic Eight Ball</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<version>1.0-groovy-2.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.4.7</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<goals>
<goal>addTestSources</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
我的规范.groovy文件:
package com.example.magic_eight_ball
import com.example.magic_eight_ball.controller.ChannelController
import org.springframework.boot.test.context.SpringBootTest
import spock.lang.Specification
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.test.web.servlet.MockMvc
@SpringBootTest
class FirstSpecification extends Specification {
@Autowired
ChannelController channelController
def "when context is loaded then all expected beans are created"() {
expect: "the ChannelController is created"
channelController
}
}
我的测试文件目录结构是:src/test/groovy/com/example/magic_eight_ball/Specification.groovy
ChannelController类的目录结构是:src/main/java/com/example/magic_eight_ball/controller/ChannelController.java
1条答案
按热度按时间vbopmzt11#
您包含了
spock-core
作为依赖项,但没有包含spock-spring
。请将依赖项更改为spock-spring
,这样您的测试应该可以工作。