Tilman Hausherr
Tilman Hausherr

Reputation: 18861

Autowired applicationContext is null in JUnit 5

I'm trying to migrate my spring applications to test with JUnit 5. It worked fine with JUnit 4. My problem is that I can't access the application context, it is null, but should be an object. I've included imports etc, in case something is wrong there.

Test:

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();
    }
}

Main:

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 (in the directory "....\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>

Output:

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

Upvotes: 2

Views: 5029

Answers (1)

M. Deinum
M. Deinum

Reputation: 124526

Tested it and tests run fine in my IDE (Intellij) running the same with your provided pom.xml fails.

You need to upgrade the maven-surefire-plugin to properly detect and execute the classes.

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

This is also expressed in the Junit5 documentation.

Upvotes: 3

Related Questions