2014-04-18 32 views
3

我已经发现三个问题应该涉及同一个问题,但不知何故,他们并没有完全解决我的问题。NoSuchMethodError与Hamcrest 1.3和GAE上的Maven

NoSuchMethodError with Hamcrest 1.3 & JUnit 4.11

NoSuchMethodError: org.hamcrest.Matchers.hasXPath when I run tests in eclipse

Getting "NoSuchMethodError: org.hamcrest.Matcher.describeMismatch" when running test in IntelliJ 10.5

单元测试迎刃而解,但它是在集成测试时RestAssured测试失败:

expect(). 
    statusCode(Status.CREATED.getStatusCode()).log().all(). 
when(). 
    post("/projects"). 
then(). 
    body("directory", Matchers.anything()).body("files", Matchers.empty()); 

其他三个相关已经提到的问题,似乎存在一些与依赖冲突有关的问题TestNG/JUnit和Hamcrest和/或Mockito,所以我尝试了各种不同的exlusions和其他内含的工件,遗憾的是无济于事。但是,有效的做法是不使用RestAssured中的log()。all(),它必定是问题的根源。因此,这将工作:

expect(). 
    statusCode(Status.CREATED.getStatusCode()). 
when(). 
    post("/projects"). 
then(). 
    body("directory", Matchers.anything()).body("files", Matchers.empty()); 

我希望能够看到,而esting虽然REST的实际输出要求,所以我很高兴有这方面的想法。谢谢!

这里的控制台输出

 java.lang.NoSuchMethodError: org.hamcrest.core.IsInstanceOf.any(Ljava/lang/Class;)Lorg/hamcrest/Matcher; 
     at org.hamcrest.Matchers.any(Matchers.java:371) 
     at com.jayway.restassured.filter.log.ResponseLoggingFilter.<init>(ResponseLoggingFilter.java:94) 
     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.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:77) 
     at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorSite.java:102) 
     at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:57) 
     at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:182) 
     at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:198) 
     at com.jayway.restassured.internal.ResponseLogSpecificationImpl.logWith(ResponseLogSpecificationImpl.groovy:85) 
     at com.jayway.restassured.internal.ResponseLogSpecificationImpl.this$3$logWith(ResponseLogSpecificationImpl.groovy) 
     at com.jayway.restassured.internal.ResponseLogSpecificationImpl$this$3$logWith.callCurrent(Unknown Source) 
     at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:49) 
     at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:133) 
     at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:145) 
     at com.jayway.restassured.internal.ResponseLogSpecificationImpl.all(ResponseLogSpecificationImpl.groovy:45) 
     at com.jayway.restassured.internal.ResponseLogSpecificationImpl.all(ResponseLogSpecificationImpl.groovy) 
     at com.jayway.restassured.specification.LogSpecification$all.callCurrent(Unknown Source) 
     at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:49) 
     at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:133) 
     at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:141) 
     at com.jayway.restassured.internal.ResponseLogSpecificationImpl.all(ResponseLogSpecificationImpl.groovy:41) 
     at com.jayway.restassured.internal.ResponseLogSpecificationImpl.all(ResponseLogSpecificationImpl.groovy) 
     at de.uniluebeck.collaboratex.test.service.ProjectServiceTests.testDeleteNonExistingProject(ProjectServiceTests.java:84) 

而且的pom.xml

 <?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> 

    <packaging>war</packaging> 
    <version>1.0-SNAPSHOT</version> 
    <groupId>de.uniluebeck.collaboratex</groupId> 
    <artifactId>CollaboraTex</artifactId> 

    <name>CollaboraTex</name> 

    <pluginRepositories> 
     <pluginRepository> 
      <id>google-staging</id> 
      <name>Google Staging</name> 
      <url>https://oss.sonatype.org/content/repositories/comgoogleappengine-1004/</url> 
     </pluginRepository> 
    </pluginRepositories> 

    <properties> 
     <appengine.target.version>1.9.2</appengine.target.version> 
     <datanucleus.version>3.1.3</datanucleus.version> 
     <jersey.version>2.6</jersey.version> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    </properties> 

    <dependencies> 
     <!-- Compile/runtime dependencies --> 
     <dependency> 
      <groupId>com.google.appengine</groupId> 
      <artifactId>appengine-api-1.0-sdk</artifactId> 
      <version>1.9.2</version> 
      <scope>compile</scope> 
     </dependency> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>servlet-api</artifactId> 
      <version>2.5</version> 
      <scope>provided</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.commons</groupId> 
      <artifactId>commons-lang3</artifactId> 
      <version>3.1</version> 
     </dependency> 

     <!-- Jersey WS-RS dependencies --> 
     <dependency> 
      <groupId>javax.ws.rs</groupId> 
      <artifactId>javax.ws.rs-api</artifactId> 
      <version>2.0</version> 
      <scope>compile</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.glassfish.jersey.core</groupId> 
      <artifactId>jersey-server</artifactId> 
      <version>${jersey.version}</version> 
      <scope>compile</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.glassfish.jersey.media</groupId> 
      <artifactId>jersey-media-multipart</artifactId> 
      <version>${jersey.version}</version> 
      <scope>compile</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.glassfish.jersey.media</groupId> 
      <artifactId>jersey-media-moxy</artifactId> 
      <version>${jersey.version}</version> 
      <scope>compile</scope> 
     </dependency> 

     <!-- JPA dependencies --> 
     <dependency> 
      <groupId>org.eclipse.persistence</groupId> 
      <artifactId>javax.persistence</artifactId> 
      <version>2.1.0</version> 
     </dependency> 
     <dependency> 
      <groupId>org.eclipse.persistence</groupId> 
      <artifactId>org.eclipse.persistence.core</artifactId> 
      <version>2.5.0</version> 
      <scope>compile</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.eclipse.persistence</groupId> 
      <artifactId>org.eclipse.persistence.moxy</artifactId> 
      <version>2.5.0</version> 
      <scope>compile</scope> 
      <type>jar</type> 
     </dependency> 

     <!-- JPA Data Nucleus dependencies --> 
     <dependency> 
      <groupId>com.google.appengine.orm</groupId> 
      <artifactId>datanucleus-appengine</artifactId> 
      <version>2.1.2</version> 
     </dependency> 
     <dependency> 
      <groupId>org.datanucleus</groupId> 
      <artifactId>datanucleus-core</artifactId> 
      <version>${datanucleus.version}</version> 
      <scope>runtime</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.datanucleus</groupId> 
      <artifactId>datanucleus-api-jpa</artifactId> 
      <version>${datanucleus.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>javax.jdo</groupId> 
      <artifactId>jdo-api</artifactId> 
      <version>3.0.1</version> 
      <scope>compile</scope> 
     </dependency> 

     <!-- needed to solve JPA Data Nucleus validation error --> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-validator</artifactId> 
      <version>5.1.0.Final</version> 
     </dependency> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>slf4j-log4j12</artifactId> 
      <version>1.5.6</version> 
     </dependency> 

     <!-- test dependencies --> 
     <dependency> 
      <groupId>com.jayway.restassured</groupId> 
      <artifactId>rest-assured</artifactId> 
      <version>2.3.1</version> 
      <scope>test</scope> 
      <exclusions> 
       <exclusion> 
        <artifactId>asm</artifactId> 
        <groupId>org.ow2.asm</groupId> 
       </exclusion> 
      </exclusions> 
     </dependency> 
     <dependency> 
      <groupId>com.jayway.restassured</groupId> 
      <artifactId>json-path</artifactId> 
      <version>2.3.1</version> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.hamcrest</groupId> 
      <artifactId>hamcrest-library</artifactId> 
      <version>1.3</version> 
      <scope>test</scope> 
      <type>jar</type> 
     </dependency> 
     <dependency> 
      <groupId>org.testng</groupId> 
      <artifactId>testng</artifactId> 
      <version>6.8.7</version> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.mockito</groupId> 
      <artifactId>mockito-all</artifactId> 
      <version>1.9.0</version> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>com.google.appengine</groupId> 
      <artifactId>appengine-testing</artifactId> 
      <version>${appengine.target.version}</version> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>com.google.appengine</groupId> 
      <artifactId>appengine-api-stubs</artifactId> 
      <version>${appengine.target.version}</version> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>com.google.appengine</groupId> 
      <artifactId>appengine-api-labs</artifactId> 
      <version>${appengine.target.version}</version> 
      <scope>test</scope> 
     </dependency> 
    </dependencies> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <version>2.5.1</version> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <configuration> 
        <source>1.7</source> 
        <target>1.7</target> 
       </configuration> 
      </plugin> 

      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-war-plugin</artifactId> 
       <version>2.3</version> 
       <configuration> 
        <archiveClasses>true</archiveClasses> 
        <webResources> 
         <!-- in order to interpolate version from pom into appengine-web.xml --> 
         <resource> 
          <directory>${basedir}/src/main/webapp/WEB-INF</directory> 
          <filtering>true</filtering> 
          <targetPath>WEB-INF</targetPath> 
         </resource> 
        </webResources> 
       </configuration> 
      </plugin> 

      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-failsafe-plugin</artifactId> 
       <version>2.6</version> 
       <configuration> 
        <includes> 
         <include>**/*ServiceTests.java</include> 
        </includes> 
       </configuration> 
       <executions> 
        <execution> 
         <id>it</id> 
         <phase>integration-test</phase> 
         <goals> 
          <goal>integration-test</goal> 
         </goals> 
        </execution> 
       </executions> 
      </plugin> 

      <plugin> 
       <groupId>com.google.appengine</groupId> 
       <artifactId>appengine-maven-plugin</artifactId> 
       <version>${appengine.target.version}</version> 
       <!-- 
       <configuration> 
        <port>7378</port> 
        <offline>true</offline> 
       </configuration> 
       --> 
       <executions> 
        <execution> 
         <id>start-gae</id> 
         <phase>pre-integration-test</phase> 
         <goals> 
          <goal>devserver_start</goal> 
         </goals> 
        </execution> 
        <execution> 
         <id>stop-gae</id> 
         <phase>post-integration-test</phase> 
         <goals> 
          <goal>devserver_stop</goal> 
         </goals> 
        </execution> 
       </executions> 
      </plugin> 

      <plugin> 
       <groupId>org.datanucleus</groupId> 
       <artifactId>maven-datanucleus-plugin</artifactId> 
       <version>${datanucleus.version}</version> 
       <configuration> 
        <api>JPA</api> 
        <verbose>true</verbose>    
        <mappingIncludes>**/entity/*.class</mappingIncludes> 
        <fork>false</fork> 
       </configuration> 
       <executions> 
        <execution> 
         <phase>process-classes</phase> 
         <goals> 
          <goal>enhance</goal> 
         </goals> 
        </execution> 
       </executions> 
      </plugin> 

     </plugins> 
    </build> 
</project> 

最后但并非最不重要的,这里是MVN依赖的输出:树

de.uniluebeck.collaboratex:CollaboraTex:war:1.0-SNAPSHOT 
+- com.google.appengine:appengine-api-1.0-sdk:jar:1.9.2:compile 
+- javax.servlet:servlet-api:jar:2.5:provided 
+- org.apache.commons:commons-lang3:jar:3.1:compile 
+- javax.ws.rs:javax.ws.rs-api:jar:2.0:compile 
+- org.glassfish.jersey.core:jersey-server:jar:2.6:compile 
| +- org.glassfish.jersey.core:jersey-common:jar:2.6:compile 
| | +- org.glassfish.jersey.bundles.repackaged:jersey-guava:jar:2.6:compile 
| | \- org.glassfish.hk2:osgi-resource-locator:jar:1.0.1:compile 
| +- org.glassfish.jersey.core:jersey-client:jar:2.6:compile 
| +- javax.annotation:javax.annotation-api:jar:1.2:compile 
| +- org.glassfish.hk2:hk2-api:jar:2.2.0:compile 
| | +- org.glassfish.hk2:hk2-utils:jar:2.2.0:compile 
| | \- org.glassfish.hk2.external:aopalliance-repackaged:jar:2.2.0:compile 
| +- org.glassfish.hk2.external:javax.inject:jar:2.2.0:compile 
| +- org.glassfish.hk2:hk2-locator:jar:2.2.0:compile 
| | \- org.javassist:javassist:jar:3.18.1-GA:compile 
| \- javax.validation:validation-api:jar:1.1.0.Final:compile 
+- org.glassfish.jersey.media:jersey-media-multipart:jar:2.6:compile 
| \- org.jvnet.mimepull:mimepull:jar:1.9.3:compile 
+- org.glassfish.jersey.media:jersey-media-moxy:jar:2.6:compile 
| +- org.glassfish.jersey.ext:jersey-entity-filtering:jar:2.6:compile 
| \- org.eclipse.persistence:org.eclipse.persistence.antlr:jar:2.5.0:compile 
+- org.eclipse.persistence:javax.persistence:jar:2.1.0:compile 
+- org.eclipse.persistence:org.eclipse.persistence.core:jar:2.5.0:compile 
| \- org.eclipse.persistence:org.eclipse.persistence.asm:jar:2.5.0:compile 
+- org.eclipse.persistence:org.eclipse.persistence.moxy:jar:2.5.0:compile 
+- com.google.appengine.orm:datanucleus-appengine:jar:2.1.2:compile 
| \- org.datanucleus:datanucleus-enhancer:jar:3.1.1:compile 
|  \- org.ow2.asm:asm:jar:4.0:compile 
+- org.datanucleus:datanucleus-core:jar:3.1.3:runtime 
+- org.datanucleus:datanucleus-api-jpa:jar:3.1.3:compile 
+- javax.jdo:jdo-api:jar:3.0.1:compile 
| \- javax.transaction:jta:jar:1.1:compile 
+- org.hibernate:hibernate-validator:jar:5.1.0.Final:compile 
| +- org.jboss.logging:jboss-logging:jar:3.1.3.GA:compile 
| \- com.fasterxml:classmate:jar:1.0.0:compile 
+- org.slf4j:slf4j-log4j12:jar:1.5.6:compile 
| +- org.slf4j:slf4j-api:jar:1.5.6:compile 
| \- log4j:log4j:jar:1.2.14:compile 
+- com.jayway.restassured:rest-assured:jar:2.3.1:test 
| +- org.codehaus.groovy:groovy:jar:2.2.1:test 
| | +- org.ow2.asm:asm-tree:jar:4.1:test 
| | +- antlr:antlr:jar:2.7.7:test 
| | +- org.ow2.asm:asm-util:jar:4.1:test 
| | +- org.ow2.asm:asm-commons:jar:4.1:test 
| | \- org.ow2.asm:asm-analysis:jar:4.1:test 
| +- org.codehaus.groovy:groovy-xml:jar:2.2.1:test 
| +- org.apache.httpcomponents:httpclient:jar:4.2.6:test 
| | +- org.apache.httpcomponents:httpcore:jar:4.2.5:test 
| | +- commons-logging:commons-logging:jar:1.1.1:test 
| | \- commons-codec:commons-codec:jar:1.6:test 
| +- org.apache.httpcomponents:httpmime:jar:4.2.6:test 
| +- org.hamcrest:hamcrest-core:jar:1.3:test 
| +- org.ccil.cowan.tagsoup:tagsoup:jar:1.2.1:test 
| \- com.jayway.restassured:xml-path:jar:2.3.1:test 
+- com.jayway.restassured:json-path:jar:2.3.1:test 
| +- org.codehaus.groovy:groovy-json:jar:2.2.1:test 
| \- com.jayway.restassured:rest-assured-common:jar:2.3.1:test 
+- org.hamcrest:hamcrest-library:jar:1.3:test 
+- org.testng:testng:jar:6.8.7:test 
| +- junit:junit:jar:4.10:test 
| +- org.beanshell:bsh:jar:2.0b4:test 
| +- com.beust:jcommander:jar:1.27:test 
| \- org.yaml:snakeyaml:jar:1.12:test 
+- org.mockito:mockito-all:jar:1.9.0:test 
+- com.google.appengine:appengine-testing:jar:1.9.2:test 
+- com.google.appengine:appengine-api-stubs:jar:1.9.2:test 
\- com.google.appengine:appengine-api-labs:jar:1.9.2:test 
+0

这是运行junit测试还是在生产中? –

+0

对不起,在运行REST服务的集成测试时,单元测试工作正常。我将它添加到了我的描述以及corssorponding REST调用中,谢谢您指出。 – Daniel

+0

是的,'测试'使库仅在单元测试中可用。 –

回答

3

TestNG依赖于JUnit。因此junit-4.10.jar在你的类路径中。这个罐子包含比Hamcrest 1.3更早的Hamcrest类,并且缺少一些方法。类加载器从JUnit加载org.hamcrest.core.IsInstanceOf,因此您遇到问题。

将依赖关系添加到JUnit 4.11到您的POM。这覆盖了TestNG对JUnit 4.10的依赖关系,并解决了Hamcrest问题,因为JUnit 4.11不再包含Hamcrest类。

<dependency> 
    <groupId>junit</groupId> 
    <artifactId>junit</artifactId> 
    <version>4.11</version> 
</dependency> 
+0

我简直不敢相信最终解决问题有多简单。实际上,我已经尝试过那个版本,但是早期版本的testng(6.8.2版本不是该版本中的最新版本,并且仍然有一个与hamcrest 1.1依赖关系的旧版本)。非常感激! – Daniel

0

至于我可以告诉仍有上暴露前hamcrest 1.3类类路径罐子,因为提到的方法是在1.3RC4只说:https://github.com/hamcrest/JavaHamcrest/commit/a6b00c4aadae733236e481fa747d5223cc7b7a9a

我建议在你喜欢的IDE,以使调试这确定它是如此。之后,我建议检查这个maven项目的依赖关系层次结构,并找出恶棍。

如果失败了,可能是jar/class存在错误,只有在文件系统中检查这个可能会有所帮助。

+0

我严重依赖Maven的**依赖项:分析**插件,删除不必要的依赖项并添加一些缺失的依赖项。我在原来的帖子中相应地更新了pom,并添加了一个代码示例。到目前为止,我发现在RestAssured中使用_log()。all()_会导致这个问题。 – Daniel