Java 问题:io.restassured.internal.http.HttpResponseException:未经授权
我是RESTAPI的新手。我尝试在IntelliJ IDEA中运行此测试函数,以测试OMDB API:Java 问题:io.restassured.internal.http.HttpResponseException:未经授权,java,rest,intellij-idea,rest-assured,Java,Rest,Intellij Idea,Rest Assured,我是RESTAPI的新手。我尝试在IntelliJ IDEA中运行此测试函数,以测试OMDB API: @Test public void shouldNotGetResponseWithoutApiKey(){ Response response = RestAssured.given() .queryParam("t", "Harry Potter") .get(&
@Test
public void shouldNotGetResponseWithoutApiKey(){
Response response = RestAssured.given()
.queryParam("t", "Harry Potter")
.get("http://www.omdbapi.com")
.then()
.statusCode(401)
.extract().response();
assertThat(response.getBody().jsonPath().getString("Error"), Matchers.containsString(errorMessage));
}
这里的逻辑是:我希望这个测试给出状态代码401,因为API密钥并没有提供给Rest Assured。这很直截了当。但我收到了这个错误:
io.restassured.internal.http.HttpResponseException: Unauthorized
at io.restassured.internal.http.HTTPBuilder.defaultFailureHandler(HTTPBuilder.java:627)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:107)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:323)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1268)
...
我搜索了错误,但找不到解决方法。如果有人能指点我,我会很高兴的
非常感谢您不应该使用restasured.given().queryParam(…).when().get(…).then(…)表单吗?那么get()之前的()是什么时候?