简体   繁体   中英

Assert boolean response with restassured SpringBoot

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.

My 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:-

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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