我已经发现了三个与同一问题有关的问题,但不知怎的,它们并没有解决我的问题。
NoSuchMethodError with Hamcrest 1.3 & JUnit 4.11
NoSuchMethodError: org.hamcrest.Matchers.hasXPath when I run tests in eclipse
单元测试工作正常,但是在RestAssured Tests失败的集成测试期间:
expect().
statusCode(Status.CREATED.getStatusCode()).log().all().
when().
post("/projects").
then().
body("directory", Matchers.anything()).body("files", Matchers.empty());
正如其他三个相关问题已经说过,似乎存在一些与TestNG / JUnit和Hamcrest和/或Mockito相互冲突的问题,所以我尝试了各种异常和所述工件的其他内容,但遗憾的是无济于事。虽然有效但不使用RestAssured中的log()。all(),它必须是问题的根源。因此,这将有效:
expect().
statusCode(Status.CREATED.getStatusCode()).
when().
post("/projects").
then().
body("directory", Matchers.anything()).body("files", Matchers.empty());
我希望尽管能够看到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 :(得分:5)
TestNG依赖于JUnit。因此junit-4.10.jar在你的类路径中。这个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>
答案 1 :(得分:0)
据我所知,类路径上的jar仍暴露出前Hamcrest 1.3类,因为上述方法仅在1.3RC4中添加:https://github.com/hamcrest/JavaHamcrest/commit/a6b00c4aadae733236e481fa747d5223cc7b7a9a
我建议您在自己喜欢的IDE中调试它,以确保它如此。之后我建议检查这个maven项目的依赖层次结构并找出恶棍。
如果失败,可能就是jar / class出错了,只在文件系统中检查这个可能对我有帮助。