简体   繁体   English

用放心的 SpringBoot 断言 boolean 响应

[英]Assert boolean response with restassured SpringBoot

I want to evaluate Boolean response coming from my Rest Controller in Spring Boot Junit Test Case. I want to evaluate Boolean response coming from my Rest Controller in Spring Boot Junit Test Case. The response seems to be the same but Rest Assured is putting brackets in the response value.响应似乎是相同的,但 Rest Assured 将括号放在响应值中。

My Controller:-我的 Controller:-

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

My Test 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}");
    }

Error while running junit test case:-运行 junit 测试用例时出错:-

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

extract the response as in the format you want exactly.something looks like below snippet.以您想要的格式提取响应。看起来像下面的代码片段。

    @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);
    }

the correct answer is正确答案是

.statusCode(HttpStatus.SC_CREATED).body(is(true)).when()

so insted of using equal use is所以使用equal使用is

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM