Maven test failed on Spring @Autowired and jUnit 5

Review the following jUnit 5 examples to test a Spring 5 MVC web application.

TestWelcome.java

package com.mkyong.web;

import com.mkyong.web.config.SpringConfig;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

@ExtendWith(SpringExtension.class)
@WebAppConfiguration
@ContextConfiguration(classes = SpringConfig.class)
public class TestWelcome {

    private MockMvc mockMvc;

    @Autowired
    private WebApplicationContext webAppContext;

    @BeforeEach
    public void setup() {
        mockMvc = MockMvcBuilders.webAppContextSetup(webAppContext).build();
    }

    @Test
    public void testWelcome() throws Exception {

        this.mockMvc.perform(
                get("/"))
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(view().name("index"))
                .andExpect(forwardedUrl("/WEB-INF/views/index.jsp"))
                .andExpect(model().attribute("msg", "Hello World"));

    }

    @Test
    public void testAbc() {
        assertEquals(2, 1 + 1);
    }

}

Tested ok with IntelliJ IDEA and Eclipse. But failed with Maven command – mvn test, look like Maven test is unable to @Autowired the WebApplicationContext class.


D:\java-web-project>mvn test

com.mkyong.web.TestWelcome.testWelcome()  Time elapsed: 0.181 sec  <<< FAILURE!
java.lang.NullPointerException
        at com.mkyong.web.TestWelcome.testWelcome(TestWelcome.java:40)
		
		
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.326 s
[INFO] Finished at: 2018-10-05T16:39:18+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test

Solution

In above console, we noticed Maven is using the maven-surefire-plugin:2.12.4, it is a bit outdated, may not support the jUnit 5 + Spring 5 integration.

To fix it, update to the latest 2.22.0

pom.xml

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

        </plugins>
    </build>

References

  1. Maven Surefire Plugin - Using JUnit 5 Platform
  2. Maven – List all the project’s plugins

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
2 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
2 Comment authors
Aastha ChopraLong Recent comment authors
newest oldest most voted
Long
Guest
Long

what is SpringConfig? Can’t find it anywhere in the post.

Aastha Chopra
Guest
Aastha Chopra

This has not resolved the issue.