【问题标题】:Spring boot integration testing issueSpring Boot 集成测试问题
【发布时间】:2021-04-25 10:02:49
【问题描述】:

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

这是控制器

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




    }

所以请帮我编写集成测试。

【问题讨论】:

  • 你尝试过什么吗?你能展示一下测试课吗?
  • 请检查一下,我已经添加了我的测试代码。

标签: java spring spring-boot spring-mvc testing


【解决方案1】:

您可以使用 MockMvcResultMatchers 将响应转换为 JSON 并检查 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));

【讨论】:

    【解决方案2】:

    @测试 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
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-24
      • 2020-02-25
      • 1970-01-01
      • 2018-09-18
      • 1970-01-01
      • 2019-05-30
      • 2015-08-20
      • 2020-04-24
      相关资源
      最近更新 更多