空的响应身体的帖子,放心

时间:2015-08-14 13:11:37

标签: java json rest junit rest-assured

我正在使用junit4放心。在我的测试方法中,我在mongodb中创建一个对象,当我运行测试时,它也成功地持久存在。但我需要存储创建的ID,所以我尝试获取响应正文。但是response.getBody().asString()是空的。

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

当我在外部调用其余端点时,它会返回响应主体以及相关字段,因此其余API没有问题。

如果对上述问题没有答案,那么你们将如何使用放心的方式测试一个带有返回体的帖子,以便我可以尝试这种方式。

我的控制器方法如下,

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

3 个答案:

答案 0 :(得分:4)

我们使用不同的wat来调用RestAssured服务。但是,如果您获得一个空字符串,则可以使用.peek()调试是否调用了您的服务。

您可以使用此测试:

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

答案 1 :(得分:1)

这是REST-Assured的亮点,其流畅的界面非常有助于找到正确的方法。如果您正在使用Spring Boot,那么测试应该可以在不添加依赖项或配置的情况下工作(当然,除了放心之外):

示例控制器

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

并测试:

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

答案 2 :(得分:0)

您可以尝试使用log()。all()方法来检查响应的内容。以下cide片段可以帮助您。

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