首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用rest保证的post响应正文为空

使用rest保证的post响应正文为空
EN

Stack Overflow用户
提问于 2015-08-14 21:11:37
回答 3查看 13.9K关注 0票数 10

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

代码语言:javascript
复制
@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");
}

当我从外部调用rest端点时,它也会返回带有相关字段的响应体,所以rest API没有问题。

如果上面的问题没有答案,那么你们将如何使用rest assured来测试带有return body的帖子,以便我可以尝试这种方式。

我的控制器方法看起来像这样,

代码语言:javascript
复制
 @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);
 }
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-08-18 03:45:51

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

您可以使用此测试:

代码语言:javascript
复制
@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);
}
票数 9
EN

Stack Overflow用户

发布于 2015-08-18 03:07:52

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

示例控制器

代码语言:javascript
复制
@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);
    }
}

和测试:

代码语言:javascript
复制
@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
EN

Stack Overflow用户

发布于 2017-03-28 14:42:39

您可以尝试使用.all()方法来检查以下response.The代码片段的内容,这可能会对您有所帮助。

代码语言:javascript
复制
@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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32010938

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档