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

时间:2018-11-14 11:39:20

标签: java spring

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类   /图书馆/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home/bin/java   (0x10bc614c0)和   /图书馆/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home/jre/lib/libinstrument.dylib   (0x10bcd14e0)。将使用两者之一。哪个未定义。

     

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

     在

  org.springframework.test.context.BootstrapUtils.createCacheAwareContextLoaderDelegate(BootstrapUtils.java:101)     在   org.springframework.test.context.BootstrapUtils.createBootstrapContext(BootstrapUtils.java:70)     在   org.springframework.test.context.TestContextManager。(TestContextManager.java:105)     在   org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTestContextManager(SpringJUnit4ClassRunner.java:152)     在   org.springframework.test.context.junit4.SpringJUnit4ClassRunner。(SpringJUnit4ClassRunner.java:143)     在sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native   方法)   sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)     在   sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)     在java.lang.reflect.Constructor.newInstance(Constructor.java:526)     在   org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)     在   org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)     在   org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)     在   org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)     在   org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)     在   org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)     在   com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:49)     在   com.intellij.rt.execution.junit.IdeaTestRunner $ Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)     在   com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)     在   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?

1 个答案:

答案 0 :(得分:1)

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

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

您正在运行一个非常异常且易于出错的设置。自己升级Spring或重新创建String filename = "matches.csv"; File file = new File(filename); try { Scanner sc = new Scanner(file); while (sc.hasNext()) { String data = sc.next(); String[] values = data.split(","); System.out.println(data); } sc.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } } 框架,然后降级到Java6。不要混用2.5和3.2依赖项,这会产生很多类路径问题,就像您刚刚观察到的那样。

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