我正在对我的Spring MVC控制器方法进行单元测试。下面是我尝试进行单元测试的方法,因为它在我启动服务器时工作正常。
每当我点击index
页面时,它会在浏览器上显示三个文本框,我在其中键入数据并按下提交按钮,然后呼叫转到addNewServers
值。
现在我需要对同样的事情进行单元测试:
@RequestMapping(value = "/index", method = RequestMethod.GET)
public Map<String, String> addNewServer() {
final Map<String, String> model = new LinkedHashMap<String, String>();
return model;
}
@RequestMapping(value = "/index", method = RequestMethod.POST)
public Map<String, String> addNewServers(@RequestParam String[] servers, @RequestParam String[] address,
@RequestParam String[] names) {
}
以下是我的junit课程:
private MockMvc mockMvc;
@Before
public void setup() throws Exception {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
this.mockMvc = standaloneSetup(new Controller()).setViewResolvers(viewResolver).build();
}
@Test
public void test04_newServers() throws Exception {
String[] servers = {"3", "3", "3"};
String[] ipaddress = {"10,20,30", "40,50,60", "70,80,90"};
String[] hostnames = {"a,b,c", "d,e,f", "g,h,i"};
// not sure how would I pass these values to my `addNewServers` method
// which is a post method call.
}
任何人都可以帮我吗?
答案 0 :(得分:4)
您只需要使用API:
mockMvc.perform(post("/index").param("servers", servers)
.param("address", ipaddress)
.param("names", hostnames))