簡體   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