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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51713975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:13:10  来源:igfitidea点击:

How to make Spring's @Autowired to work in JUnit 5 extensions?

javaspringspring-bootjunitjunit5

提问by Theemathas Chirananthavat

I have a Spring Boot application, and I am trying to use @Autowiredin a JUnit 5 extension. However, I cannot get it to work. (The @Autowiredfield is null.) Can anybody help?

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

Below is code that demonstrates the problem I'm having (the important parts are SomeExtensionand SomeTest. As written, mvn testcauses the test to fail in beforeEach. Sorry if I'm including too much.

下面是演示我遇到的问题的代码(重要的部分是SomeExtensionSomeTest。正如所写的那样,mvn test导致测试失败beforeEach。对不起,如果我包含的太多了。

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

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:

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

src/main/java/somepackage/SomeBean.java

package somepackage;

import org.springframework.stereotype.Component;

@Component
public class SomeBean {
}

src/main/java/somepackage/MainClass.java

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:

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>

I'm also having similar issues with @Value. If the solution also works for that, it would be great.

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

Thank you.

谢谢你。

回答by Nicolai

JUnit 5 extensionscan not operate on other extensions, just on test classes.

JUnit 5 扩展不能对其他扩展进行操作,只能对测试类进行操作。

So...

所以...

@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() {
    }

}

... can not work. This would:

...不能工作。这个会:

public class SomeExtension implements BeforeEachCallback {

    @Override
    public void beforeEach(ExtensionContext context) {
        // [...]
    }

}

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

    @Autowired
    SomeBean bean;

    @Test
    void nothingTest() {
    }

}

If you can explain why you need a bean in your extension, we may be able to help you find a fix for that, too.

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