我目前正在做一个Spring项目,我想写一些测试。不幸的是,我不知道如何通过所需的数组。
Get Request工作得很好...下面是我的代码:
MainControllerTest.java
private MockMvc mockMvc;
@InjectMocks
private MainController controller;
@Before
public void setUp() throws Exception {
mockMvc = MockMvcBuilders.standaloneSetup(controller)
.build();
}
@Test
public void resetAll() throws Exception {
String[] players = new String[] {"Player 1", "Player 2"};
mockMvc.perform(
MockMvcRequestBuilders.get("http://localhost:8443/api/reset")
)
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().string("false"));
mockMvc.perform(
MockMvcRequestBuilders.post("http://localhost:8443/api/player").content(players)
);
}
MainController.java
@PostMapping("/player")
public void setPlayersList(@RequestBody String[] players) {
for(int i = 0; i<players.length; i++) {
playersList.add(players[i]);
}
System.out.println(Arrays.toString(playersList.toArray()));
}
1条答案
按热度按时间5f0d552i1#
延迟回答总比没有回答好。下面是可以实现这个目的的代码。这里cacheNames是
Array<String>
,并且在Controller中也请求了这种类型。Java注意事项:
.apply
与创建一个示例,然后用它调用.addAll
相同。