Reputation: 397
Can you please advise who to test Collection with JUnit 4 and Spring MVC?
Controller
@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";
}
}
Testing a Person class can be done by:
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")))));
}
}
But I am not able to figure out how to test if perService.getPerson return List!
Upvotes: 0
Views: 1095
Reputation: 3448
@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");
}
Upvotes: 4
Reputation: 279970
You'd test it the same way you are doing now but using corresponding Matcher
instances to check Collection
elements.
Assuming you had a handler method like
@RequestMapping(value = { "/persons",}, method = RequestMethod.GET)
public String getPersons(Model model) {
model.addAttribute("personList", personService.getPersons());
return "personsPage";
}
you simply need to change your mock to return something else
when(personService.getPersons()).thenReturn( // get some Person objects in a List
Arrays.asList(new Person(1L, "StackOverflow"), new Person(1L,
"StackOverflow")));
and perform the new request with a Matcher
that does a check appropriate to the elements you return in your List
. For example
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"))))));
Upvotes: 0