1

I have controller:

@Controller
public class EventMenuController{

    @RequestMapping(value = "/updateEvent", method = RequestMethod.POST)
    public String updateEvent(Model model,
            @Valid @ModelAttribute("existedEvent") Event event,
            BindingResult result,
            @ModelAttribute("linkedCandidates") Set<Candidate> candidates,
            @ModelAttribute("linkedvacancies") Set<Vacancy> vacancies,
            @RequestParam(required = true, value = "selectedEventStatusId")Integer EventStatusId,
            @RequestParam(required = true, value = "selectedEventTypeId")Integer EventTypeId ,
            RedirectAttributes attributes) {
        if (result.hasErrors()) {
            //model.addAttribute("idEvent", event.getId());
            event.setCandidates(candidates);
            event.setVacancies(vacancies);
            return "eventDetails";
        }
        eventService.updateEventAndLinkedEntities(event, candidates, vacancies ,EventTypeId,EventStatusId);
        attributes.addAttribute("idEvent",event.getId() );//event is null therefore NPE here
        attributes.addAttribute("message", "submitted correctly at "+new Date());
        return "redirect:eventDetails";
     }
 }

For testing this method I wrote following class:

@ContextConfiguration(locations = { "classpath:/test/BeanConfigUI.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class EventMenuControllerTest {


    @Test
    public void updateEvent() throws Exception{
        MockHttpServletRequestBuilder request = MockMvcRequestBuilders
                .post("/updateEvent");
        request.param("selectedEventStatusId", "1");
        request.param("selectedEventTypeId", "1");

        EventMenuController eventMenuController = (EventMenuController) wac.getBean("eventMenuController");
        EventService mockEventService = Mockito.mock(EventService.class);
        eventMenuController.eventService = mockEventService;

        Mockito.doNothing().when(mockEventService).updateEventAndLinkedEntities(any(Event.class), any(Set.class),any(Set.class), any(Integer.class), any(Integer.class));

        ResultActions result = mockMvc.perform(request);

        result.andExpect(MockMvcResultMatchers.view().name("redirect:eventDetails"));
        result.andExpect(MockMvcResultMatchers.model().attributeExists("idEvent"));
        result.andExpect(MockMvcResultMatchers.model().attributeExists("message"));

    }

}

In the process request executing on server side I see error that show me that event object is null.

Question:

What request I must write that pass event to server-side(controller method) using MockMvc ?

1 Answers1

0

The Event class object is not initialized. You can either create an Event object or create a mock of an Event object depending on your test case and send it to the EventMenuController class object. You can do this similar to how you have sent a mock object of EventService to EventMenuController.

It is better practice to use fields as part of a class and not as part of a method. This will give you flexibility to mock any field.

AdityaTS
  • 89
  • 11
  • "You can either create an Event object or create a mock of an Event object depending on your test case and send it to the EventMenuController class object" How can I make so? –  Oct 17 '13 at 08:49
  • "You can do this similar to how you have sent a mock object of EventService to EventMenuController" I don't understand how –  Oct 17 '13 at 08:50
  • event is a local variable! –  Oct 17 '13 at 08:51
  • If you can make Event object a member of the EventMenuController class you can replace it with a mock object using a getter method. – AdityaTS Oct 17 '13 at 08:52
  • But if I don't make it I have other ways ? –  Oct 17 '13 at 09:00
  • You cannot access or modify a field declared in a method. Such a practice makes the code rigid and testing harder. – AdityaTS Oct 17 '13 at 09:03
  • I don't sure that it is great idea - make event as field of controller –  Oct 17 '13 at 09:05