【问题标题】:What went wrong on RestAssured API callRestAssured API 调用出了什么问题
【发布时间】:2018-10-25 06:17:01
【问题描述】:

我正在使用以下代码在我的本地计算机上托管的 Jira 上创建问题。 但我收到错误代码 400。我无法找出问题所在,并且在从邮递员发出 API 调用时一切正常。

我正在使用以下位置提供的 API 文档 - https://developer.atlassian.com/server/jira/platform/jira-rest-api-examples/

public static 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);


}

}

线程“main”中的异常 java.lang.AssertionError: 1 期望失败。 预期状态代码 但为 。

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)

【问题讨论】:

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


    【解决方案1】:

    查看您的邮递员屏幕截图,我可以看到您只传递了标题和正文,但您放心的代码似乎有点过多地填充了其他引用,代码 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);
    }
    

    【讨论】:

      猜你喜欢
      • 2017-12-14
      • 1970-01-01
      • 1970-01-01
      • 2021-06-19
      • 1970-01-01
      • 2012-04-13
      • 1970-01-01
      • 1970-01-01
      • 2011-10-30
      相关资源
      最近更新 更多