简体   繁体   中英

Exception in thread "main" java.lang.NoClassDefFoundError: org/hamcrest/Matchers

Getting this error while running this code for RestAssured:

Exception in thread "main" java.lang.NoClassDefFoundError: org/hamcrest/Matchers
    at io.restassured.internal.ResponseSpecificationImpl.statusCode(ResponseSpecificationImpl.groovy:133)
    at io.restassured.internal.ValidatableResponseOptionsImpl.statusCode(ValidatableResponseOptionsImpl.java:119)
    at newclas.main(newclas.java:18)
Caused by: java.lang.ClassNotFoundException: org.hamcrest.Matchers
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 3 more

This is the piece of code that I'm running on my system :Not sure how to resolve the import issue. Have tried several imports but doesn't work.

//import io.restassured.matcher.RestAssuredMatchers.*;
//import io.restassured.matcher.RestAssuredMatchers;
import static io.restassured.RestAssured.given;
import io.restassured.RestAssured;
import static org.hamcrest.Matchers.equalTo;
public class newclas {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        RestAssured.baseURI="https://jsonplaceholder.typicode.com";
        System.out.println("befoire given");
        given().
        header("Content-Type", "application/json").
        when().
        get("/users").
        then().
        assertThat().statusCode(200);
        System.out.println("thank you ++ %d");


    }

}

org.hamcrest.Matchers is not available in you class path

Add below dependency to your project

<!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-junit -->
<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest-junit</artifactId>
    <version>2.0.0.0</version>
    <scope>test</scope>
</dependency>

This exception tells you that at runtime your class path doesn't contain the hamcrest library.

In other words: you managed to have that library showing up in the classpath for the "compile" step, but you somehow forgot about when running your code.

Thus the answer is to step back and ensure that the classpath setup that gets applied during compile matches the one used for running your compiled classes. So, either you do that manually, or you look into the definition of your project within your IDE or build tool.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM