繁体   English   中英

Spring开机集成测试问题

[英]Spring boot integration testing issue

我正在做一个项目,我已经完成了,但问题是在项目中我必须编写集成测试代码。

这是 controller

 @PostMapping("/newgame")
    public ResponseEntity<Players> beginNewGame(@RequestBody Players players) {

        Players savedPersion = playerService.beginNewGame(players);

        return savedPersion!=null ?  ResponseEntity.ok(savedPersion):
                (ResponseEntity<Players>) ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR);
}

这就是服务。

@Override
    public Players beginNewGame(Players players) {

            //new game state
            GameState gameState =new GameState();
            gameState.setWinner("null");


        GameState save = gameStateRepositery.save(gameState);

        //new score
        Score score = new Score();
        score.setGameId(save.getId());

        Score savedScore = scoreRepositery.save(score);
        players.setScoreId(savedScore.getId());
        players.setGameId(save.getId());

        Players savedPlayers=null;
            savedPlayers= playersRepositery.save(players);

        System.out.println(savedScore.getGameId()+"ksdjfkjskn");

       return savedPlayers;
    }

如果我用适当的必需参数点击端点,则工作正常,然后返回给我。

{
    "gameId": 57,
    "id": 1,
    "playerOne": "vinitSaini",
    "playerTwo": "anonymousKal",
    "scoreId": 58
}

这是测试代码,我如何检查响应返回的内容。

@ExtendWith(SpringExtension.class)
@SpringBootTest
@AutoConfigureMockMvc
public class PlayersIntTest {

    @Autowired
    private MockMvc mockMvc;

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private PlayersRepositery playersRepositery;


    @Test
    void newGame() throws Exception {
        
        //mockMvc.perform(post("/api/{forumId}/register", 42L)
        mockMvc.perform(post("/api/newgame")
                .contentType("application/json")
                .content(objectMapper.writeValueAsString(players)))
                .andExpect(status().isOk());




    }

所以请帮我写集成测试。

您可以转换 JSON 中的响应并使用MockMvcResultMatchers检查 JSON 属性的值

mockMvc.perform(post("/api/newgame")
       .contentType("application/json")
       .content(objectMapper.writeValueAsString(players)))
       .andExpect(status().isOk())
       .andExpect(MockMvcResultMatchers.content()
          .contentType(MediaType.APPLICATION_JSON))
       .andExpect(MockMvcResultMatchers.jsonPath("$.gameId")
          .value(57))
       .andExpect(MockMvcResultMatchers.jsonPath("$.id")
          .value(1))
       .andExpect(MockMvcResultMatchers.jsonPath("$.playerOne")
          .value("vinitSaini"))
       .andExpect(MockMvcResultMatchers.jsonPath("$.playerTwo")
          .value("anonymousKal"))
       .andExpect(MockMvcResultMatchers.jsonPath("$.scoreId")
          .value(58));

@Test void newGame() 抛出异常 {

    //mockMvc.perform(post("/api/{forumId}/register", 42L)
    ResultActions resultActions = mockMvc.perform(post("/api/newgame")
            .contentType("application/json")
            .content(objectMapper.writeValueAsString(players)))
            .andExpect(status().isOk());
    String responseInJson = resultActions.andReturn().getResponse().getContentAsString();
    Players responseObj = objectMapper.readValue(responseInJson, Players.class);
    Assert.assertNotNull(responseObj);
    // You can add all asserts here


}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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