Home > Enterprise >  Autowired Controller in Spring Boot test is null
Autowired Controller in Spring Boot test is null

Time:05-17

I am trying my hand at creating tests for my Spring boot app using Spock/Groovy. I am following a guide, and the first test is to test whether all the beans are created. I auto wired one of my controller classes, ChannelController, and expected it to be created. However, the test is showing that it's actually null. I created some other simple tests in the test file (1 1 == 2) and they all run and pass. Can anyone help me out?

Here is my 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>

My Specification.groovy file:

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

My directory structure for the test file is: src/test/groovy/com/example/magic_eight_ball/Specification.groovy

My directory structure for the ChannelController class is:

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

CodePudding user response:

You included spock-core as a dependency, but not spock-spring. Change your dependency to say spock-spring instead, and your test should work.

  • Related