gpt4 book ai didi

java - 如何在 Spring MVC 2.5 项目中使用 MockMvc?

转载 作者:行者123 更新时间:2023-11-30 05:55:15 25 4
gpt4 key购买 nike

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]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home/bin/java (0x10bc614c0) and /Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home/jre/lib/libinstrument.dylib (0x10bcd14e0). One of the two will be used. Which one is undefined.

java.lang.IllegalStateException: Could not load CacheAwareContextLoaderDelegate [class org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]

at org.springframework.test.context.BootstrapUtils.createCacheAwareContextLoaderDelegate(BootstrapUtils.java:101) at org.springframework.test.context.BootstrapUtils.createBootstrapContext(BootstrapUtils.java:70) at org.springframework.test.context.TestContextManager.(TestContextManager.java:105) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTestContextManager(SpringJUnit4ClassRunner.java:152) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.(SpringJUnit4ClassRunner.java:143) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:526) at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104) at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86) at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59) at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26) at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59) at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:49) at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) Caused by: java.lang.NoSuchMethodError: org.springframework.beans.BeanUtils.instantiateClass(Ljava/lang/Class;Ljava/lang/Class;)Ljava/lang/Object; at org.springframework.test.context.BootstrapUtils.createCacheAwareContextLoaderDelegate(BootstrapUtils.java:98) ... 18 more

Process finished with exit code 255

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

最佳答案

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

  • Full Java 6 and Java EE 5 support (JDBC 4.0, JTA 1.1, JavaMail 1.4, JAX-WS 2.0)

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

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

关于java - 如何在 Spring MVC 2.5 项目中使用 MockMvc?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53299390/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com