用restassuredspringbootAssert布尔响应

kknvjkwl  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(397)

我想在springbootjunit测试用例中评估来自rest控制器的布尔响应。响应似乎是相同的,但请放心,在响应值中加了括号。
我的controller:-

@PutMapping("/file/{id}")
ResponseEntity<Boolean> uploadFile(@RequestBody MultipartFile file,
        @PathVariable String id) {
        return new ResponseEntity<>(
                fileService.uploadImage(file, id),
                HttpStatus.CREATED);
    }

我的测试case:-

@Test
    public void UploadAttachmentTest() throws Exception {
        given().pathParam("id", "randomId").multiPart(dummyFile).expect()
                .statusCode(HttpStatus.SC_CREATED).body(equalTo(true)).when()
                .put("/file/{id}");
    }

运行junit测试时出错case:-

java.lang.AssertionError: 1 expectation failed.
Response body doesn't match expectation.
Expected: <true>
  Actual: true

    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)
....
7eumitmz

7eumitmz1#

按照您想要的格式提取响应。

@Test
    public void UploadAttachmentTest() throws Exception {
        Boolean response = given()
                          .pathParam("id", "randomId")
                          .multiPart(dummyFile)
                          .expect()
                          .statusCode(HttpStatus.SC_CREATED)
                          .extract().response().as(Boolean.class);

        // Here u can check response with assertTrue() or assertFalase()
        assertTrue(response);
    }

相关问题