2014-03-30 85 views
0

能否请您指教谁使用JUnit 4和Spring MVC测试集?Junit的 - 春天 - 测试集合

控制器

@Controller 
public class PersonController { 
    @Autowired 
    private PersonService personService; 

    @RequestMapping(value = {"/", "/index"}, method = RequestMethod.GET) 
    public String getPerson(Model model) { 
     model.addAttribute("personData", personService.getPerson); 
     return "personPage"; 
    } 
} 

测试人的类可以这样做:

public class TestPersonController { 

    @Mock 
    private PersonService personService; 

    @InjectMocks 
    private PersonController personController; 

    private MockMvc mockMvc; 

    @Before 
    public void setup() { 
     MockitoAnnotations.initMocks(this); 
     mockMvc = MockMvcBuilders.standaloneSetup(personController).build(); 
    } 

    @Test 
    public void testGetPerson() throws Exception { 
     when(personService.getPerson(1L)).thenReturn(new Person(1L, "StackOverflow")); 

     mockMvc.perform(get("/person/{id}", 1L)) 
       .andExpect(status().isOk()) 
       .andExpect(view().name("personPage")) 
       .andExpect(model().attribute("personData", 
              allOf(hasProperty("id", is(1L)), 
                hasProperty("name", is("StackOverflow"))))); 
    } 
} 

但我无法弄清楚,如果perService.getPerson返回列表如何测试!

+0

让我们看看你的控制器。 –

+0

另外,你有什么尝试测试'List'返回的对象? –

+0

@SotiriosDelimanolis - 我添加了控制器代码。对不起,我不知道该怎么做,我试图运行相同的Junit代码,并且(如预期的)失败。 – Sandeep

回答

0

您会按照与现在相同的方式对其进行测试,但使用相应的Matcher实例检查Collection元素。

假设你有一个像

@RequestMapping(value = { "/persons",}, method = RequestMethod.GET) 
public String getPersons(Model model) { 
    model.addAttribute("personList", personService.getPersons()); 
    return "personsPage"; 
} 

一个处理方法,你只需要改变你的模拟返回别的东西

when(personService.getPersons()).thenReturn(// get some Person objects in a List 
     Arrays.asList(new Person(1L, "StackOverflow"), new Person(1L, 
       "StackOverflow"))); 

,并执行与Matcher,做适当的检查了新的要求您在List中返回的元素。例如

mockMvc.perform(get("/persos")) 
     .andExpect(status().isOk()) 
     .andExpect(view().name("personsPage")) 
     .andExpect(
       model().attribute(
         "personList", 
         Matchers.everyItem(AllOf.allOf(
           HasPropertyWithValue.hasProperty("id", Is.is(1L)), 
           HasPropertyWithValue.hasProperty("name", Is.is("StackOverflow")))))); 
4
@Autowired 
private PersonController personController; 

private MockHttpServletRequest request; 
private MockHttpServletResponse response; 
private PersonService personService; 

@Before 
public void setup() { 
    request = new MockHttpServletRequest(); 
    response = new MockHttpServletResponse(); 
    personService = createNiceMock(PersonService.class); 
} 
@Test 
public void shouldCheckRequestMappingForUserDashBoard() throws Exception { 
    request.setRequestURI("/index"); 
    request.setMethod("GET"); 
    modelAndView = new AnnotationMethodHandlerAdapter().handle(request, response, personController); 

    // you can call according to what you want to create mock. 
    expect(personService.getPerson()).andReturn(new Person()); 
    replay(personService); 

    Assert.assertNotNull(modelAndView.getViewName()); 
    Assert.assertEqual(modelAndView.getViewName(),"personPage"); 
}