An empty response body for a message with confidence

I am using restassured with junit4. In my testing method, I create an object in mongodb, and when I run the test, it is saved successfully. But I need to save the created identifier so that I try to get the response body. But response.getBody().asString() empty.

 @Test public void testA() throws JSONException { Map<String,Object> createVideoAssignmentParm = new HashMap<String,Object>(); createVideoAssignmentParm.put("test1", "123"); Response response = expect().statusCode(201).when().given().contentType("application/json;charset=UTF-8") .headers(createVideoAssignmentParm).body(assignment).post("videoAssignments"); JSONObject jsonObject = new JSONObject(response.getBody().asString()); id= (String)jsonObject.getString("assignmentId"); } 

When I call the external endpoint from the outside, it returns the response body also with the corresponding fields, so there is no problem with the rest of the API.

If there is no answer for the above question, then how do you guys check the message with the return body using the rest so that I can try this.

My controller method looks like

  @RequestMapping(value = "/videoAssignment", produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE, method = RequestMethod.POST) @ResponseBody public HttpEntity<VideoAssignment> createVideoAssingnment( //@ApiParam are there..){ //other methods return new ResponseEntity<>(va, HttpStatus.CREATED); } 
+5
source share
3 answers

We use another wat to call services using RestAssured. However, if you get an empty string, you can debug whether your service has been called or not using .peek() .

You can use this test:

 @Test public void testStatus() { String response = given() .contentType("application/json") .body(assignment) .when() .post("videoAssignments") .peek() // Use peek() to print the ouput .then() .statusCode(201) // check http status code .body("assignmentId", equalTo("584")) // whatever id you want .extract() .asString(); assertNotNull(response); } 
+4
source

In this case, REST-Assured shines, its free interface is very useful for finding the right method to use. If you use Spring Boot, the test should work without adding dependencies or configuration (except for the assurance that :)

Controller example

 @RestController @RequestMapping("/api") public class Endpoints { public static class Assignment { public int id = 1; public String name = "Example assignment"; } @RequestMapping(value = "/example", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<Assignment> example(@RequestBody Assignment assignment) { return ResponseEntity.created(URI.create("/example/1")) .body(assignment); } } 

and test:

 @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebAppConfiguration @IntegrationTest("server.port:0") public class EndpointsTest { @Autowired private ObjectMapper objectMapper; @Value("${local.server.port}") private int port; @Before public void setUp() { RestAssured.port = port; } @Test public void exampleTest() throws Exception { Endpoints.Assignment assignment = given() .contentType(ContentType.JSON) .body(objectMapper.writeValueAsBytes(new Endpoints.Assignment())) .when() .post("/api/example") .then().statusCode(HttpStatus.SC_CREATED) .extract().response() .as(Endpoints.Assignment.class); // We can now save the assignment.id assertEquals(1, assignment.id); } } 
+1
source

You can try using the log () method. all () to check the contents of the response. This snippet can help you.

 @Test public void test(){ Map<String,Object> createVideoAssignmentParm = new HashMap<String,Object>(); createVideoAssignmentParm.put("test1", "123"); Response response=given() .spec(request) .contentType(ContentType.JSON) .body(assignment) .post("videoAssignments"); response.then() .statusCode(201).log().all(); assignmentId=response.path("assignmentId"); } 
0
source

All Articles