在为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
这是我在我的系统上运行的一段代码:不确定如何解决导入问题。尝试了几次进口但不起作用。
//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在您的课程路径中不可用
在项目中添加以下依赖项
<!-- 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>
此异常告诉您,在运行时,您的类路径不包含hamcrest库。
换句话说:您设法将该库显示在类路径中以进行“编译”步骤,但是您在运行代码时忘了这一点。
因此,答案是退后一步,确保在编译期间应用的类路径设置与用于运行已编译类的类路径设置匹配。因此,您可以手动执行此操作,也可以在IDE或构建工具中查看项目的定义。