我已经找到了三个应该与同一问题相关的问题,但不知何故它们并没有完全解决我的问题。
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 的依赖冲突似乎存在一些问题,所以我尝试了各种排除和所述 Artifact 的其他包含,不幸的是无济于事。有效的是不在 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 依赖项的输出:tree
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
最佳答案
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>
关于java - 在 GAE 上使用 Hamcrest 1.3 和 Maven 的 NoSuchMethodError,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23162642/