Java Spring引导集成测试问题
我正在做一个项目,我已经完成了,但问题是在项目中我必须编写集成测试代码 这是控制器 如果我使用适当的必需参数命中端点,则 工作很好,还我 这是测试代码,如何检查响应是什么 返回Java Spring引导集成测试问题,java,spring,spring-boot,spring-mvc,testing,Java,Spring,Spring Boot,Spring Mvc,Testing,我正在做一个项目,我已经完成了,但问题是在项目中我必须编写集成测试代码 这是控制器 如果我使用适当的必需参数命中端点,则 工作很好,还我 这是测试代码,如何检查响应是什么 返回 因此,请帮助我编写集成测试。您可以将响应转换为JSON,并使用 您可以转换JSON中的响应,并使用 @试验 void newGame()引发异常{ //mockMvc.perform(post("/api/{forumId}/register", 42L) ResultActions
因此,请帮助我编写集成测试。您可以将响应转换为JSON,并使用
您可以转换JSON中的响应,并使用 @试验 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
}
@试验
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
}
你试过什么吗?你能显示测试类吗?请检查我现在添加了我的测试代码。你试过什么吗?你能显示测试类吗?请检查我现在添加了我的测试代码。
{
"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());
}
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));
//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
}