【问题标题】:NoSuchMethodError while using MockHttpServletRequest - JUnit使用 MockHttpServletRequest 时出现 NoSuchMethodError - JUnit
【发布时间】:2016-09-14 00:37:41
【问题描述】:

当我尝试像这样实例化 MockHttpServletRequest 对象时出现以下错误: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)

我的 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>

我的外部库目录:

我还添加了一个 Java Servlet jar:

我正在使用 Intellij IDEA;提前致谢。

【问题讨论】:

  • 您正在混合 Spring 2.0.8 和 4.2.6 罐子。永远不要混合框架的版本。将 spring-mock 替换为 spring-test 并使用与主 spring 版本相同的版本。
  • @M.Deinum 非常感谢;这似乎可以解决它。
  • @M.Deinum 谢谢你!您的评论回答了这个问题。您应该将其添加为可接受的答案:它非常有帮助。

标签: spring maven junit dependency-injection mockito


【解决方案1】:

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

致谢:M. Deinum

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-28
    • 2014-09-18
    • 2012-01-05
    • 1970-01-01
    相关资源
    最近更新 更多