简体   繁体   English

带有正文的 RestAssured Post 调用会引发错误“java.lang.AssertionError:1 期望失败。 预期状态代码 <200> 但为 <415>。”

[英]RestAssured Post call with body throws an error “java.lang.AssertionError: 1 expectation failed. Expected status code <200> but was <415>.”

@Test
public void testPost() throws URISyntaxException {
    AgencyRequest agencyRequest = new AgencyRequest();
    agencyRequest.setConnectorId(1);
    agencyRequest.setJobConfig("/usr/local/workspace/test.config.xml");
    agencyRequest.setConnectorName("/usr/local/workspace/test.kjb");
    agencyRequest.setRequestId(1);
    agencyRequest.setTenantId(1);
    agencyRequest.setTenantName("Test Tenant");
    agencyRequest.setTimeZone("UTC");

    String json = new Gson().toJson(agencyRequest);
    System.out.println(json);
    System.out.println(uri + "/" + ResourceConstants.JOB_CONFIG);
    given().
    accept(ContentType.JSON).
    body(json).
    post(new URI(uri + "/" + ResourceConstants.JOB_CONFIG)).
    then().
    assertThat().
    statusCode(HttpStatus.OK_200);
}

If I run the same test on Postman by choosing the post method and content type as json and with the body, it gave the response status code as 200. But my unit test is not passed.如果我通过选择 post 方法和内容类型作为 json 并使用正文在 Postman 上运行相同的测试,它给出的响应状态代码为 200。但我的单元测试没有通过。 In the body, I have tried passing the json string as you see before as well as I tried passing the java object, both fails.在正文中,我尝试传递您之前看到的 json 字符串以及尝试传递 java 对象,但都失败了。

You need to define the content type of request while sending the request.您需要在发送请求时定义请求的内容类型。 Consider making the given change考虑进行给定的更改

   .contentType(ContentType.JSON)

Try adding this line to the top of your code:尝试将此行添加到代码的顶部:

.log().all()
        .config(RestAssured.config().encoderConfig(encoderconfig.appendDefaultContentCharsetToContentTypeIfUndefined(false)))
.header()....

这个改变会起作用,试试吧。

.header("Content-Type", "application/json").contentType(ContentType.JSON).accept(ContentType.JSON)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.AssertionError:预期状态:&lt;200&gt;但原为:&lt;405&gt; - java.lang.AssertionError: Status expected:<200> but was:<405> java.lang.AssertionError:预期状态:&lt;200&gt;但原为:&lt;302&gt; itShouldAllowAccessToSecuredPageForPermittedUser - java.lang.AssertionError: Status expected:<200> but was:<302> itShouldAllowAccessToSecuredPageForPermittedUser @RequestMapping java.lang.AssertionError:预期状态:200 实际:404 - @RequestMapping java.lang.AssertionError: Status Expected :200 Actual :404 java.lang.AssertionError: Status expected:&lt;200&gt; but was:&lt;404&gt; in Junit test - java.lang.AssertionError: Status expected:<200> but was:<404> in Junit test java.lang.AssertionError:预期状态:&lt;200&gt;但原为:&lt;201&gt; - java.lang.AssertionError: Status expected:<200> but was:<201> java.lang.AssertionError:预期状态:&lt;200&gt;但原为:&lt;404&gt; - java.lang.AssertionError: Status expected:<200> but was:<404> java.lang.AssertionError:预期状态:&lt;200&gt;,但在静态服务中为:&lt;404&gt; - java.lang.AssertionError: Status expected:<200> but was:<404> in restful services java.lang.AssertionError: 预期状态:&lt;200&gt; 但为:&lt;400&gt; - java.lang.AssertionError: Status expected:<200> but was:<400> java.lang.AssertionError:预期状态:&lt;400&gt; 但原为:&lt;200&gt; 预期:400 实际:200 - java.lang.AssertionError: Status expected:<400> but was:<200> Expected :400 Actual :200 测试反应式存储库 java.lang.AssertionError:期望“assertNext”失败(预期:onNext();实际:onComplete()) - Testing reactive repository java.lang.AssertionError: expectation "assertNext" failed (expected: onNext(); actual: onComplete())
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM