简体   繁体   English

MockMvc。 如何在junit测试中包含自定义对象

[英]MockMvc. how to involve a custom object in junit test

I need the JUnit test to check the controller. 我需要JUnit测试来检查控制器。 The method of the controller is called by submit a form. 控制器的方法通过提交表单来调用。 This method receives a filled object EmailDispatchForm form . 此方法接收一个已填充的对象EmailDispatchForm form

Here is a snippet from controller: 这是控制器的片段:

@RequestMapping(value={"/sendEmail"}, method=POST)
public String sendEmail(EmailDispatchForm form,Model model ){
    String status = "OK";

    try {
        workFlows.sendEmailAndRecordHistory(form);
    } catch (Exception e) {
        status = "ERROR";
        model.addAttribute("errDescription", e.getCause().getMessage());
        logger.error(e, e);
    } finally {
        model.addAttribute("sentEmailStatus", status);
    }

    return CommonConstants.VIEW_EMAIL_SENT;
}

This is my junit test: 这是我的junit测试:

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = {WebConfig.class})
public class HomeControllerTest {

    @Autowired
    private WebApplicationContext webApplicationContext;

    private MockMvc mockMvc;

    @Before
    public void setup() {
        mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
    }

    @Test
    public void testShowEmailForm() throws Exception{
        mockMvc.perform(get("/emailForm")).andExpect(view().name(CommonConstants.VIEW_EMAIL));
    }
}

How you can see, in the controller there is the method sendEmail() , which has two parameter: EmailDispatchForm form , Model model . 可以看到,在控制器中有方法sendEmail() ,它有两个参数: EmailDispatchForm formModel model I have no idea how can I involve this both parameters in junit test to utilize it as ie Mock-Objects. 我不知道如何在junit测试中同时包含这两个参数,以将其用作模拟对象。

I would be very thankful for assistance and would like to know about some resources (books) to study the subject of this issue. 我非常感谢您的帮助,并想了解一些资源(书籍)来研究这个问题。

The first problem is that you are trying to use GET and acces a POST URL. 第一个问题是您尝试使用GET并访问POST URL。 Your test method should look like this: 您的测试方法应如下所示:

  @Test
    public void testShowEmailForm() throws Exception{
        mockMvc.perform(post("/sendEmail")).andExpect(view().name(CommonConstants.VIEW_EMAIL));
    }

Also add @RequestBody in your controller method 还要在控制器方法中添加@RequestBody

public String sendEmail(@RequestBody EmailDispatchForm form,Model model )

To add the form simply add to the test method: 要添加表单,只需将其添加到测试方法中:

.contentType(MediaType.APPLICATION_JSON)
        .content(json)

Where JSON is the JSON form of the `EmailDispatchForm form` object. JSON是`EmailDispatchForm form`对象的JSON形式。

Hope this helps. 希望这可以帮助。

so, i found the other solution. 因此,我找到了另一种解决方案。 It looks like this: 它看起来像这样:

  @Test public void loginUser() throws Exception { LoginForm loginForm = new LoginForm(); loginForm.setPassword("admin"); loginForm.setUserId("admin"); RequestBuilder request = post("/authentication") .flashAttr("loginForm", loginForm); mockMvc .perform(request) .andDo(MockMvcResultHandlers.print()) .andExpect(redirectedUrl("emailForm")); } 
for the method of controller 用于控制器的方法

  @RequestMapping(value={"/authentication"}, method=POST) public String toAuthentication(@ModelAttribute("loginForm")LoginForm loginForm, Model model){ String view ="redirect:emailForm"; // some activity here to take the loginForm and process it return view; } 

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

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