繁体   English   中英

Junit-春季-测试合集

[英]Junit - Spring - Testing Collections

您能建议谁用JUnit 4和Spring MVC测试Collection吗?

调节器

@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";
    }
} 

测试Person类可以通过以下方式完成:

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是否返回List!

@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");
}

您将以与现在相同的方式进行测试,但使用相应的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执行新请求,该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"))))));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM