我如何使用放心的方式自动化多部分表单数据?

时间:2019-05-06 12:03:17

标签: java rest-assured

下面是curl命令,用于发送POST请求以添加布局。它包含一个需要上传的文件和一个JSON正文。

命令:

curl -X POST  "http://localhost:8080/space-management/layout" -H "Accept: application/json" -H "Content-Type: multipart/form-data" -F "layoutRequest={\"companyName\":\"XXXX\",\"countryName\":\"India\",\"cityName\":\"Kadapa\",\"buildingName\":\"KSR\",\"floorLevel\":112,\"deskSize\":10};type=application/json" -F "file=@/C:/Users/vraghumanda/Downloads/Ascendas 4th Floor.jpg"

成功运行后,将产生以下响应。

{
    "context": {
        "headers": {},
        "entity": {
            "apiStatus": {
                "executionStatus": "success"
            },
            "locationId": 153
        },
        "entityType": "com.altimetrik.space_spec.AddLayoutResponse",
        "entityAnnotations": [],
        "entityStream": {
            "closed": false,
            "committed": false
        },
        "length": -1,
        "language": null,
        "location": null,
        "date": null,
        "lastModified": null,
        "links": [],
        "entityTag": null,
        "stringHeaders": {},
        "requestCookies": {},
        "acceptableLanguages": [
            "*"
        ],
        "responseCookies": {},
        "committed": false,
        "mediaType": null,
        "allowedMethods": [],
        "entityClass": "com.altimetrik.space_spec.AddLayoutResponse",
        "acceptableMediaTypes": [
            {
                "type": "*",
                "subtype": "*",
                "parameters": {},
                "quality": 1000,
                "wildcardSubtype": true,
                "wildcardType": true
            }
        ]
    },
    "status": 201,
    "length": -1,
    "language": null,
    "location": null,
    "date": null,
    "lastModified": null,
    "entity": {
        "apiStatus": {
            "executionStatus": "success"
        },
        "locationId": 153
    },
    "metadata": {},
    "statusInfo": "CREATED",
    "links": [],
    "entityTag": null,
    "stringHeaders": {},
    "cookies": {},
    "mediaType": null,
    "allowedMethods": [],
    "headers": {}
}

有人可以建议如何使用放心的电话来自动执行上述休息电话。

这是我的尝试

@测试 公共无效addLayout(){

HashMap<String, Object> data = new HashMap<>();
data.put("companyName", "Altimetrik");
data.put("countryName", "India");
data.put("cityName", "Kadapa");
data.put("buildingName", "KSR");
data.put("floorLevel", 3);
data.put("deskSize", 10);

RestAssured.baseURI="http://localhost:8080/space-management/";
RequestSpecBuilder requestSpecBuilder = new RequestSpecBuilder();
requestSpecBuilder.addMultiPart("file", System.getProperty("user.dir")+"\\layoutFiles\\148_Ascendas 13th Floor.jpg").
        addFormParams(data);
RequestSpecification requestSpecification = requestSpecBuilder.build();
given().
        spec(requestSpecification).
when().
        post("layout").
then().
        assertThat().statusCode(200);

}

但是我最终得到了400个错误,而不是200个响应代码。 请在下面的堆栈跟踪中找到。

17:18:48.698 [main] DEBUG org.apache.http.headers - << HTTP/1.1 400 
17:18:48.698 [main] DEBUG org.apache.http.headers - << Vary: Origin
17:18:48.698 [main] DEBUG org.apache.http.headers - << Vary: Access-Control-Request-Method
17:18:48.698 [main] DEBUG org.apache.http.headers - << Vary: Access-Control-Request-Headers
17:18:48.698 [main] DEBUG org.apache.http.headers - << Content-Type: application/json;charset=UTF-8
17:18:48.698 [main] DEBUG org.apache.http.headers - << Transfer-Encoding: chunked
17:18:48.698 [main] DEBUG org.apache.http.headers - << Date: Mon, 06 May 2019 11:48:48 GMT
17:18:48.698 [main] DEBUG org.apache.http.headers - << Connection: close
17:18:48.719 [main] DEBUG io.restassured.internal.RequestSpecificationImpl$RestAssuredHttpBuilder - Parsing response as: application/json;charset=UTF-8
17:18:48.719 [main] DEBUG io.restassured.internal.RequestSpecificationImpl$RestAssuredHttpBuilder - Parsed data to instance of: class org.apache.http.conn.EofSensorInputStream

java.lang.AssertionError: 1 expectation failed.
Expected status code <200> but was <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:80)
    at org.codehaus.groovy.reflection.CachedConstructor.doConstructorInvoke(CachedConstructor.java:74)
    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:237)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:249)
    at io.restassured.internal.ResponseSpecificationImpl$HamcrestAssertionClosure.validate(ResponseSpecificationImpl.groovy:494)
    at io.restassured.internal.ResponseSpecificationImpl$HamcrestAssertionClosure$validate$1.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:115)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:127)
    at io.restassured.internal.ResponseSpecificationImpl.validateResponseIfRequired(ResponseSpecificationImpl.groovy:656)
    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.PlainObjectMetaMethodSite.doInvoke(PlainObjectMetaMethodSite.java:43)
    at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSiteNoUnwrapNoCoerce.invoke(PogoMetaMethodSite.java:190)
    at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:58)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:51)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:156)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:168)
    at io.restassured.internal.ResponseSpecificationImpl.statusCode(ResponseSpecificationImpl.groovy:125)
    at io.restassured.specification.ResponseSpecification$statusCode$0.callCurrent(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:51)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:156)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:168)
    at io.restassured.internal.ResponseSpecificationImpl.statusCode(ResponseSpecificationImpl.groovy:133)
    at io.restassured.internal.ValidatableResponseOptionsImpl.statusCode(ValidatableResponseOptionsImpl.java:119)
    at com.altimetrik.spacemanagement.restScripts.AddLayout.addLayout(AddLayout.java:37)
    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.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:648)
    at org.testng.TestRunner.run(TestRunner.java:505)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
    at org.testng.SuiteRunner.run(SuiteRunner.java:364)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
    at org.testng.TestNG.runSuites(TestNG.java:1049)
    at org.testng.TestNG.run(TestNG.java:1017)
    at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:73)
    at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)

谢谢。

0 个答案:

没有答案