Spring @Autowired和jUnit 5上的Maven测试失败

本文档介绍了在Maven环境中遇到的Spring @Autowired与jUnit 5测试失败的问题。问题在于Maven使用的Surefire插件版本过旧,不支持jUnit 5 + Spring 5的集成。解决方法是更新Maven Surefire插件至最新版本2.22.0,以确保兼容性。
摘要由CSDN通过智能技术生成

查看以下jUnit 5示例,以测试Spring 5 MVC Web应用程序。

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

}

使用IntelliJ IDEA和Eclipse进行了测试。 但是通过Maven命令– mvn test失败,看起来Maven测试无法@Autowired WebApplicationContext类。

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

在上面的控制台中,我们注意到Maven正在使用maven-surefire-plugin:2.12.4 ,这有点过时了,可能不支持jUnit 5 + Spring 5集成。

要修复它 ,请更新到最新的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>

参考文献

  1. Maven Surefire插件–使用JUnit 5平台
  2. Maven –列出项目的所有插件

翻译自: https://mkyong.com/maven/maven-test-failed-on-spring-autowired-and-junit-5/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值