Lucifer Morningstar
Lucifer Morningstar

Reputation: 139

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

Upvotes: 2

Views: 3344

Answers (2)

Ewa Przybylska
Ewa Przybylska

Reputation: 26

use is instead of equal:

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

Upvotes: 1

Tamil.S
Tamil.S

Reputation: 483

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

Upvotes: 0

Related Questions