2014-03-04 4 views
2

스프링 MVC 컨트롤러 메소드에서 유닛 테스트를하고 있습니다. 아래는 내 서버를 시작할 때 단위 테스트를하려고하는 나의 방법입니다.junit을하는 동안 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

당신이 봤어을? http://stackoverflow.com/questions/9995807/spring-mvc-unit-tests-can-i-pass-url-with-params-to-controller?rq=1 – mttdbrd

답변

4
당신은 단지 API 사용할 필요가

:

mockMvc.perform(post("/index").param("servers", servers) 
           .param("address", ipaddress) 
           .param("names", hostnames)) 
+3

왜 이것이 다운 voted 되었습니까? 어떤 특별한 이유? – AKIWEB

+0

양식 매개 변수는 어떻게됩니까? – Sajad