使用MockHttpServletRequest时出现NoSuchMethodError-JUnit [英] NoSuchMethodError while using MockHttpServletRequest - JUnit

查看:129
本文介绍了使用MockHttpServletRequest时出现NoSuchMethodError-JUnit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试实例化MockHttpServletRequest对象时,出现以下错误: MockHttpServletRequest request = new MockHttpServletRequest();

I am getting the following error when I am trying to instantiate a MockHttpServletRequest object like this: MockHttpServletRequest request = new MockHttpServletRequest();


java.lang.NoSuchMethodError:org.springframework.core.CollectionFactory.createLinkedMapIfPossible(I)Ljava / util / Map;在org.springframework.mock.web.MockHttpServletRequest。( MockHttpServletRequest.java:107)
在org.springframework.mock.web.MockHttpServletRequest。(MockHttpServletRequest.java:187)

java.lang.NoSuchMethodError: org.springframework.core.CollectionFactory.createLinkedMapIfPossible(I)Ljava/util/Map;at org.springframework.mock.web.MockHttpServletRequest.(MockHttpServletRequest.java:107) at org.springframework.mock.web.MockHttpServletRequest.(MockHttpServletRequest.java:187)

我的Maven代码:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>groupId</groupId>
    <artifactId>Servlet</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
    </properties>
    <dependencies>

        <!-- http://mvnrepository.com/artifact/org.springframework/spring-web -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.2.6.RELEASE</version>
        </dependency>
        <!-- http://mvnrepository.com/artifact/org.springframework/spring-mock -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-mock</artifactId>
            <version>2.0.8</version>
        </dependency>
    </dependencies>
</project>

iml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
  <component name="FacetManager">
    <facet type="web" name="Web">
      <configuration>
        <descriptors>
          <deploymentDescriptor name="web.xml" url="file://$MODULE_DIR$/web/WEB-INF/web.xml" />
        </descriptors>
        <webroots>
          <root url="file://$MODULE_DIR$/web" relative="/" />
        </webroots>
        <sourceRoots />
      </configuration>
    </facet>
  </component>
  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5" inherit-compiler-output="false">
    <output url="file://$MODULE_DIR$/target/classes" />
    <output-test url="file://$MODULE_DIR$/target/test-classes" />
    <content url="file://$MODULE_DIR$">
      <sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
      <sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
      <sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
      <excludeFolder url="file://$MODULE_DIR$/target" />
    </content>
    <orderEntry type="inheritedJdk" />
    <orderEntry type="sourceFolder" forTests="false" />
    <orderEntry type="library" name="Java EE 6-Java EE 6" level="project" />
    <orderEntry type="library" name="Maven: org.springframework:spring-web:4.2.6.RELEASE" level="project" />
    <orderEntry type="library" name="Maven: org.springframework:spring-aop:4.2.6.RELEASE" level="project" />
    <orderEntry type="library" name="Maven: aopalliance:aopalliance:1.0" level="project" />
    <orderEntry type="library" name="Maven: org.springframework:spring-beans:4.2.6.RELEASE" level="project" />
    <orderEntry type="library" name="Maven: org.springframework:spring-context:4.2.6.RELEASE" level="project" />
    <orderEntry type="library" name="Maven: org.springframework:spring-expression:4.2.6.RELEASE" level="project" />
    <orderEntry type="library" name="Maven: org.springframework:spring-core:4.2.6.RELEASE" level="project" />
    <orderEntry type="library" name="Maven: org.springframework:spring-mock:2.0.8" level="project" />
    <orderEntry type="library" name="Maven: commons-logging:commons-logging:1.1" level="project" />
  </component>
</module>

我的外部库目录:

< a href = https://i.stack.imgur.com/IHLjF.png rel = noreferrer>

我还添加了一个Java Servlet jar:

I have also added a Java Servlet jar:

< a href = https://i.stack.imgur.com/ZLJdJ.png rel = noreferrer>

我正在使用Intellij IDEA;

I am using Intellij IDEA; thanks in advance.

推荐答案

M。 Deinum响应对我有用,用 spring-test 替换 spring-mock ,另外看看.iml文件并确保弹簧模拟物已经消失,否则请手动将其删除并重新尝试执行测试。

M. Deinum response worked for me, replace spring-mock with spring-test, additionally take a look at the .iml file and make sure the spring-mock is gone, otherwise remove it manually and re-attempt to execute the test.

信用:M. Deinum

Credits to: M. Deinum

这篇关于使用MockHttpServletRequest时出现NoSuchMethodError-JUnit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆