junit 5中的autowired applicationcontext为空

wtlkbnrh  于 2021-07-23  发布在  Java
关注(0)|答案(1)|浏览(483)

我正在尝试将我的spring应用程序迁移到junit5上进行测试。在JUnit4上运行得很好。我的问题是我不能访问应用程序上下文,它是空的,但应该是一个对象。我已经包括了进口等,以防那里出了问题。
测试:

package com.mycompany.mavenspringjunit5;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

@SpringJUnitConfig(locations = "/test-config.xml")
public class MainTest
{
    @Autowired
    ApplicationContext applicationContext;
    @Autowired
    Integer number;

    // fails
    @Test
    public void testGivenAppContext_WhenInjected_ThenItShouldNotBeNull()
    {
        System.out.println("number: " + number);
        Assertions.assertNotNull(applicationContext, "applicationContext should not be null");
    }

    // works
    @Test
    public void testDoStuff()
    {
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/test-config.xml");
        Main instance = ctx.getBean(Main.class);
        instance.doStuff();
    }
}

主要内容:

package com.mycompany.mavenspringjunit5;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

@Component
public class Main
{
    @Autowired
    Integer number;
    @Autowired
    ApplicationContext applicationContext;

    public static void main(String[] args)
    {
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        ctx.getBean(Main.class).doStuff();
    }

    void doStuff()
    {
        System.out.println("The number is " + number);
        System.out.println("applicationContext " + applicationContext);
    }
}

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>com.mycompany</groupId>
    <artifactId>MavenSpringJunit5</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <spring.version>5.3.4</spring.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.8.0-M1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

test-config.xml(在“…\mavenspringjunit5\src\test\resources\test config.xml”目录下):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.3.xsd">

    <context:annotation-config />

    <context:component-scan base-package="com.mycompany.mavenspringjunit5"/>

    <bean id="number" class="java.lang.Integer">
        <constructor-arg value="42" />
    </bean>

</beans>

输出:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.mycompany.mavenspringjunit5.MainTest
The number is 42
applicationContext org.springframework.context.support.ClassPathXmlApplicationContext@51521cc1, started on Mon Feb 22 10:49:15 CET 2021
number: null
Tests run: 2, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.884 sec <<< FAILURE!
com.mycompany.mavenspringjunit5.MainTest.testGivenAppContext_WhenInjected_ThenItShouldNotBeNull()  Time elapsed: 0.009 sec  <<< FAILURE!
org.opentest4j.AssertionFailedError: applicationContext should not be null ==> expected: not <null>
    at org.junit.jupiter.api.AssertionUtils.fail(AssertionUtils.java:39)
    at org.junit.jupiter.api.Assertions.fail(Assertions.java:118)
    at org.junit.jupiter.api.AssertNotNull.failNull(AssertNotNull.java:47)
    at org.junit.jupiter.api.AssertNotNull.assertNotNull(AssertNotNull.java:36)
    at org.junit.jupiter.api.Assertions.assertNotNull(Assertions.java:292)
    at com.mycompany.mavenspringjunit5.MainTest.testGivenAppContext_WhenInjected_ThenItShouldNotBeNull(MainTest.java:24)
5hcedyr0

5hcedyr01#

在我的ide(intellij)中对它进行了测试,测试运行良好,与您提供的相同 pom.xml 失败。
你需要升级 maven-surefire-plugin 正确地检测和执行类。

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.2</version>
        </plugin>
    </plugins>
</build>

junit5文档中也表达了这一点。

相关问题