RestAssured API调用出了什么问题

时间:2018-10-25 06:17:01

标签: automated-tests rest-assured web-api-testing rest-assured-jsonpath

我正在使用以下代码在本地计算机上托管的Jira上创建问题。 但是我收到的错误代码为400。在从邮递员进行API调用时,我无法找出问题出在哪里,并且一切工作都很好。

我正在使用以下位置的API文档进行此操作- https://developer.atlassian.com/server/jira/platform/jira-rest-api-examples/

enter image description here enter image description here

公共静态void main(String [] args)抛出MalformedURLException,FileNotFoundException {

    RestAssured.baseURI = "http://localhost:8080";
    String headerKey=null;
    String headerValue = null;

    String body = "{\r\n" + 
            "    \"fields\": {\r\n" + 
            "       \"project\":\r\n" + 
            "       {\r\n" + 
            "          \"key\": \"GOOG\"\r\n" + 
            "       },\r\n" + 
            "       \"summary\": \"REST ye mereeery gentlemen.\",\r\n" + 
            "       \"description\": \"Creating of an eeissue using project keys and issue type names using the REST API\",\r\n" + 
            "       \"issuetype\": {\r\n" + 
            "          \"name\": \"Bug\"\r\n" + 
            "       }\r\n" + 
            "   }\r\n" + 
            "}";

    Map<String,String> authInfo = AuthenticateUser.getSessionInfo();
    for(String key:authInfo.keySet()) {
        System.out.println(key+":"+authInfo.get(key));
        headerKey = key;
        headerValue = authInfo.get(key);

    }

    given().header(headerKey,headerValue).body(body).contentType(ContentType.JSON).
    when().post("/rest/api/2/issue/").
    then().statusCode(201);


}

}

线程“主”中的异常java.lang.AssertionError:1个期望失败。 预期状态码为<201>,但为<400>。

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:83)
at org.codehaus.groovy.reflection.CachedConstructor.doConstructorInvoke(CachedConstructor.java:77)
at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrap.callConstructor(ConstructorSite.java:84)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:59)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:238)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:250)
at io.restassured.internal.ResponseSpecificationImpl$HamcrestAssertionClosure.validate(ResponseSpecificationImpl.groovy:483)
at io.restassured.internal.ResponseSpecificationImpl$HamcrestAssertionClosure$validate$1.call(Unknown Source)
at io.restassured.internal.ResponseSpecificationImpl.validateResponseIfRequired(ResponseSpecificationImpl.groovy:655)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSiteNoUnwrapNoCoerce.invoke(PogoMetaMethodSite.java:210)
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:59)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:169)
at io.restassured.internal.ResponseSpecificationImpl.statusCode(ResponseSpecificationImpl.groovy:123)
at io.restassured.specification.ResponseSpecification$statusCode$0.callCurrent(Unknown Source)
at io.restassured.internal.ResponseSpecificationImpl.statusCode(ResponseSpecificationImpl.groovy:131)
at io.restassured.internal.ValidatableResponseOptionsImpl.statusCode(ValidatableResponseOptionsImpl.java:119)
at com.local.jira.Demo.main(Demo.java:46)

1 个答案:

答案 0 :(得分:0)

看看您的邮递员屏幕截图,我可以看到您只传递了标头和正文,但是您放心的代码似乎填充了其他引用,代码400为BAD_REQUEST

我已经设计了一个示例代码,该代码应该会有所帮助。如果您仍然需要帮助,请还原

{
    RequestSpecification req = RestAssured.given();
    req.header("Content-Type", "application/json");
    req.header("JSESSIONID", "9533");

    req.body("{\n" +
        "    \"fields\": {\n" +
        "       \"project\":\n" +
        "       {\n" +
        "          \"key\": \"TEST\"\n" +
        "       },\n" +
        "       \"summary\": \"REST ye merry gentlemen.\",\n" +
        "       \"description\": \"Creating of an issue using project keys and issue type names using the REST API\",\n" +
        "       \"issuetype\": {\n" +
        "          \"name\": \"Bug\"\n" +
        "       }\n" +
        "   }\n" +
        "}");

    Response resp = req.post("http://localhost:8080/rest/api/2/issue");

    int code = resp.getStatusCode();
    System.out.println("Status Code is : " + code);
    String body = resp.asString();
    System.out.println("Body is : " + body);
    Assert.assertEquals(code, 201);
}