java Spring MVC 4 控制器的 Junit 测试用例以检查 @RequestMapping 中的 @PathVariable 参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31937870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 19:23:19  来源:igfitidea点击:

Junit Test cases for Spring MVC 4 controller to check @PathVariable parameters in @RequestMapping

javaspringspring-mvcjunit

提问by Nishant Kumar

I have following controller code for which I have to write JUnit test case.

我有以下控制器代码,我必须为其编写 JUnit 测试用例。

public class EquipmentController {

    private Map<String, Equipment> equiList = new HashMap <String,Equipment>();

    @RequestMapping("/rest/equipment/{Number}")
    public Equipment getEquipment(@PathVariable String Number){

        if(!equiList.containsKey(Number)){
            lNumber = DEFAULT;
        }
        return equiList.get(Number);

    }
}

I'm writing the JUnit test case for the same as below:

我正在编写 JUnit 测试用例,如下所示:

import static org.springframework.test.web.ModelAndViewAssert.*;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({/* include live config here
    e.g. "file:web/WEB-INF/application-context.xml",
    "file:web/WEB-INF/dispatcher-servlet.xml" */})
public class EquipmentControllerTest {

    @Inject
    private ApplicationContext applicationContext;

    private MockHttpServletRequest request;
    private MockHttpServletResponse response;
    private HandlerAdapter handlerAdapter;
    private EquipmentController controller;

    @Before
    public void setUp() {
       request = new MockHttpServletRequest();
       response = new MockHttpServletResponse();
       handlerAdapter = applicationContext.getBean(HandlerAdapter.class);
       // Get the controller from the context here
       controller = new EquipmentController();
    }

    @Test
    public void testgetEquipment() throws Exception {
       request.getUriString()("lNumber");
       final Equipment equip = handlerAdapter.handle(request, response, 
           controller);
       assertViewName(equip, "view");
    }
}

But am not sure if this test classis correct or not as I am new to JUnit.

但是我不确定这个测试类是否正确,因为我是 JUnit 的新手。

Can anyone please suggest how to do this.

任何人都可以请建议如何做到这一点。

回答by Branislav Lazic

Create a mock of your controller and use MockMvcto test your methods:

创建您的控制器的模拟并用于MockMvc测试您的方法:

import static org.springframework.test.web.ModelAndViewAssert.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({/* include live config here
    e.g. "file:web/WEB-INF/application-context.xml",
    "file:web/WEB-INF/dispatcher-servlet.xml" */})
public class EquipmentControllerTest {

    private MockMvc mockMvc;

    private EquipmentController controller;

    @Before
    public void setUp() {

       this.mockMvc = MockMvcBuilders.standaloneSetup(equipmentController).build()
    }

    @Test
    public void testgetEquipment() throws Exception {
      this.mockMvc.perform(get("/rest/equipment/{Number}", 3))
           .andExpect(status().isOk())
    }
}

where "3" represents value of your path variable.

其中“3”代表路径变量的值。