首页 > 解决方案 > 如何在 Spring MVC 2.5 项目中使用 MockMvc?

问题描述

MockMvc 在 Spring Test 3.2 中引入。我有一个依赖于 Spring MVC 2.5 的旧项目,它很难升级(有太多事情要解决)。我想使用 MockMvc 进行集成测试。

像这样的代码:

import static org.hamcrest.CoreMatchers.containsString;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.io.IOException;

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
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 redis.embedded.RedisServer;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/afdspplicationContext*.xml",
        "/applicationContext*.xml"})
@WebAppConfiguration
public class ScheduleControllerTest {
    private static RedisServer redisServer;
    @Autowired
    private WebApplicationContext wac;

    private MockMvc mockMvc;
    @BeforeClass
    public static void setUp() throws IOException {

        redisServer = new RedisServer(45678);
        redisServer.start();
    }

    @Before
    public void setup() throws Exception {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
    }

    @AfterClass
    public static void tearDown() {
        redisServer.stop();
    }
    @Test
    public void getRoomStatusByDateAndBuilding() throws Exception {

        this.mockMvc.perform(get("/api/v1/recommend/checkin?userName=dd&roomName=f"))
                .andExpect(status().isOk())
                .andExpect(content().string(containsString("\"code\":422")))
                .andExpect(content().string(containsString("test")));
    }
}

但是,测试以错误结束:

objc[77723]:JavaLaunchHelper 类在 /Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home/bin/java (0x10bc614c0) 和 /Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/ 中实现内容/主页/jre/lib/libinstrument.dylib (0x10bcd14e0)。将使用两者之一。哪一个是未定义的。

java.lang.IllegalStateException:无法加载 CacheAwareContextLoaderDelegate [class org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]

execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) 原因:java.lang.NoSuchMethodError: org.springframework.beans。 BeanUtils.instantiateClass(Ljava/lang/Class;Ljava/lang/Class;)Ljava/lang/Object; 在 org.springframework.test.context.BootstrapUtils.createCacheAwareContextLoaderDelegate(BootstrapUtils.java:98) ... 还有 18 个

进程以退出代码 255 结束

可能由于 Spring MVC 2.5 与 Spring Test 4.3.9 不匹配而发生此错误。是否存在任何解决方法,让我在继续使用 Spring MVC 2.5 的同时使用 MockMvc?

标签: javaspring

解决方案


Spring 2.5 于 11 年前(2007 年 11 月 19 日)发布,并未正式支持您尝试使用的 Java 7。根据Spring Framework 2.5 发布

  • 完整的 Java 6 和 Java EE 5 支持(JDBC 4.0、JTA 1.1、JavaMail 1.4、JAX-WS 2.0)

您正在运行一个非常不寻常且容易出错的设置。升级 Spring 或MockMvc自己重新创建和向后移植框架并降级到 Java 6。不要混合 2.5 和 3.2 依赖项,这会产生很多你刚才观察到的类路径问题。

我认为 11 年后升级 Spring 并不是开发人员时间最糟糕的投资。


推荐阅读