1

I am getting the error for ResponseLoggingFilter in Rest Assured framework while running the code with RestAssured java libraries. I am using the 4.5.1 rest assured version in the POM file. Has anybody faced the similar issue? Need inputs.

**Error:**

java.lang.NoSuchMethodError: 'org.hamcrest.Matcher org.hamcrest.core.IsInstanceOf.any(java.lang.Class)'

at org.hamcrest.Matchers.any(Matchers.java:349)
at io.restassured.filter.log.ResponseLoggingFilter.<init>(ResponseLoggingFilter.java:73)
at io.restassured.filter.log.ResponseLoggingFilter.logResponseTo(ResponseLoggingFilter.java:182)

**Framework Utility:**

public RequestSpecification setBaseURI() throws IOException {
    if (requestSpec == null) {
        baseURL = System.getProperty("baseURL", config.getConfig().getProperty("BASE_URL"));
        log.info(baseURL);
        config.setProperty("APIBase.properties");
        RestAssured.urlEncodingEnabled = false;
        PrintStream log = new PrintStream(new FileOutputStream("log.txt"));
        requestSpec = new RequestSpecBuilder().
                setBaseUri(config.getConfig().getProperty("BASE_URL"))
                .addFilter(RequestLoggingFilter.logRequestTo(log))
                .addFilter(ResponseLoggingFilter.logResponseTo(log))
                .build();
    }
    return requestSpec;
}

**POM.xml**

 <dependency>
         <groupId>io.rest-assured</groupId>
         <artifactId>rest-assured</artifactId>
         <version>4.5.1</version>
 </dependency>

 <dependency>
         <groupId>io.rest-assured</groupId>
         <artifactId>rest-assured-common</artifactId>
         <version>4.5.1</version>
</dependency>
  • Most probably there is another embedded version of `org.hamcrest.Matcher` that has more priority at run time than the one used at compile time. You need to share the all your dependencies in POM.xml. Otherwise, you could diagnose it yourself by running `mvn dependency:tree` and look for `hamcrest` dependencies, then use dependencies exclusions to get rid of the version of hamcrest that is causing this conflict. – Mohamed Ennahdi El Idrissi Apr 25 '22 at 04:57

1 Answers1

1

According to the stacktrace, missing method has this signature:

org.hamcrest.Matcher org.hamcrest.core.IsInstanceOf.any(java.lang.Class)

According to the POM file, rest-assured 4.5.1 has hamcrest 2.1 as a dependency.

In Hamcrest 2.1, the any method is declared as:

public static <T> Matcher<T> any(Class<T> type) {
    return (Matcher<T>) new IsInstanceOf(type);
}

The erased type signature will be org.hamcrest.Matcher any(java.lang.Class) ... which is exactly what the JVM is looking for.

Stranger, and stranger ...

Somehow the JVM is loading a version of Hamcrest that doesn't have that particular any method. But according to the Git history ... all official versions of Hamcrest have an any method with that signature, all the way back to Hamcrest 1.0. Indeed, the method hasn't changed between 1.0 and 2.2 ... which is the latest official release.


Are you sure that you are getting the Rest-Assured and Hamcrest JARs from a reliable place?

The next thing to do would be to examine the JARs that you are actually using, check their versions and where they came from, unpack them, and examine the respective ".class" files forensically with a javap.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216