Spring 中的自动装配控制器启动测试为空

Autowired Controller in Spring Boot test is null

我正在尝试使用 Spock/Groovy 为我的 Spring 启动应用程序创建测试。我正在按照指南进行操作,第一个测试是测试是否创建了所有 bean。我自动连接了我的一个控制器 classes,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>

我的 Specification.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 class 目录结构是:

src/main/java/com/example/magic_eight_ball/controller/ChannelController.java

您将 spock-core 作为依赖项包含在内,但未包含 spock-spring。将您的依赖项更改为 spock-spring,您的测试应该可以工作。