如何使Spring的@Autowired在JUnit 5扩展中工作?

时间:2018-08-06 18:53:40

标签: java spring spring-boot junit junit5

我有一个Spring Boot应用程序,并且试图在JUnit 5扩展中使用@Autowired。但是,我无法使其正常工作。 (@Autowired字段为空。)有人可以帮忙吗?

下面的代码演示了我遇到的问题(重要的部分是SomeExtensionSomeTest。按照书面规定,mvn test导致测试在beforeEach中失败。对不起,如果我加入的内容过多。

src / test / java / somepackage / SomeExtension.java:

package somepackage;

import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import static org.junit.jupiter.api.Assertions.assertNotNull;

@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SomeExtension implements BeforeEachCallback {
    @Autowired
    SomeBean bean;

    @Override
    public void beforeEach(ExtensionContext context) {
        assertNotNull(bean);
    }
}

src / test / java / somepackage / SomeTest.java:

package somepackage;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;


@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ExtendWith(SomeExtension.class)
class SomeTest {
    @Test
    void nothingTest() {
    }
}

src / main / java / somepackage / SomeBean.java

package somepackage;

import org.springframework.stereotype.Component;

@Component
public class SomeBean {
}

src / main / java / somepackage / MainClass.java

package somepackage;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MainClass {
    public static void main(String[] args) {
        SpringApplication.run(MainClass.class, args);
    }
}

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>fooGroupId</groupId>
    <artifactId>barArtifactId</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <!-- Don't include Junit 4 -->
                    <groupId>junit</groupId>
                    <artifactId>junit</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.2.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <!--
                This is copied from https://junit.org/junit5/docs/current/user-guide/#running-tests-build-maven
                This allows the surefire plugin to be able to find Junit 5 tests, so `mvn test` works.
                -->
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.21.0</version>
                <dependencies>
                    <dependency>
                        <groupId>org.junit.platform</groupId>
                        <artifactId>junit-platform-surefire-provider</artifactId>
                        <version>1.2.0</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-releases</id>
            <url>https://repo.spring.io/libs-release</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-releases</id>
            <url>https://repo.spring.io/libs-release</url>
        </pluginRepository>
    </pluginRepositories>

</project>

我也遇到@Value的类似问题。如果该解决方案也适用于此,那就太好了。

谢谢。

1 个答案:

答案 0 :(得分:1)

JUnit 5 extensions不能在其他扩展程序上运行,只能在测试类上运行。

所以...

let result = Object.keys(this.headers).map((key) => this.headers[key])

...无法工作。这将:

@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SomeExtension implements BeforeEachCallback {

    @Autowired
    SomeBean bean;

    @Override
    public void beforeEach(ExtensionContext context) {
        assertNotNull(bean);
    }

}

@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ExtendWith(SomeExtension.class)
class SomeTest {

    @Test
    void nothingTest() {
    }

}

如果您可以解释为什么扩展中需要一个bean,我们也可以帮助您找到解决方法。