2016-05-17 14 views
1

私は私はこのようなMockHttpServletRequestオブジェクトをインスタンス化しようとしています、次のエラーを取得しています:MockHttpServletRequest request = new MockHttpServletRequest();NoSuchMethodError MockHttpServletRequestを使用している間 - JUnitの

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> 

私の外部ライブラリディレクトリ:

enter image description here

私は、Javaサーブレットの瓶をも追加しました:

enter image description here

私はのIntelliJ IDEAを使用しています。前もって感謝します。

+6

あなたは春の2.0.8と4.2.6のjarファイルを混合しています。フレームワークのバージョンを混在させないでください。 'spring-mock'を' spring-test'に置き換え、あなたのメインスプリングバージョンと同じバージョンを使用してください。 –

+0

@ M.Deinumありがとうございました。それはそれを修正するようだ。 –

+0

@ M.Deinum私からもありがとう!あなたのコメントはこの質問に答えます。受け入れられた答えとして追加する必要があります。非常に便利です。 – mginius

答えて

0

M. Deinumの応答が働いてspring-mockspring-testに置き換え、さらに.imlファイルを見て、スプリングモックがなくなっていることを確認してください。そうでなければ手動で削除してテストを再実行してください。

クレジットに:M. Deinum

関連する問題