1

私はコントローラーを持っています:

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

このメソッドをテストするために、次のクラスを作成しました。

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

    }

}

サーバー側で実行されているプロセス リクエストで、イベント オブジェクトが null であることを示すエラーが表示されます。

質問:

MockMvc を使用してサーバー側 (コントローラー メソッド) にイベントを渡すには、どのような要求を記述する必要がありますか?

4

1 に答える 1