【问题标题】:Junit Test cases for Spring MVC 4 controller to check @PathVariable parameters in @RequestMapping用于 Spring MVC 4 控制器的 Junit 测试用例检查 @RequestMapping 中的 @PathVariable 参数
【发布时间】:2015-11-03 10:45:45
【问题描述】:

我有以下控制器代码,我必须为其编写 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);

    }
}

我正在编写与以下相同的 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");
    }
}

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

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

【问题讨论】:

    标签: java spring spring-mvc junit


    【解决方案1】:

    创建一个你的控制器的模拟并使用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())
        }
    }
    

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

    【讨论】:

    • 感谢@Branislav 的回复。您的意思是这样的吗:RunWith(SpringJUnit4ClassRunner.class) public class EquipmentControllerTest { Before public void setUp() { EquipmentController equipmentController;模拟Mvc 模拟Mvc; this.mockMvc = MockMvcBuilders.standaloneSetup(equipmentController).build(); } 测试 public void testgetEquipment(){ this.mockMvc.perform(get("/rest/equipment/{lNumber}", 3)) .andExpect(status().isOk()); } }
    • 非常感谢@Branislav。如果 RequestMapping("/rest/getInventoryBySoldToCustomer/{customerId}/{countryCode}") 那么测试方法可以是这样的,请告诉我:Test public void testgetInventoryDe​​atilsResponse() throws Exception { this.mockMvc.perform(get("/休息/getInventoryBySoldToCustomer/{customerId}/{countryCode}", 3,8)) .andExpect(status().isOk()); }
    • @nish 根据documentation 判断,是的,您可以。由于get 方法采用可变数量的参数。
    • 我的课是这样的: RestController public class InventoryController { Autowired private JdbcTemplate jdbcTemplate; RequestMapping("/rest/getInventoryBySoldToCustomer/{customerId}/{countryCode}") public Inventory getInventoryDe​​atilsResponse( PathVariable String customerId, PathVariable String countryCode) { Inventory inventory = initInventory();退回库存; } 私有库存 initInventory() { 库存库存 = new Inventory(); //做一些返回库存的事情;}}
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 2014-12-01
    • 1970-01-01
    • 2018-02-23
    • 2010-11-26
    • 1970-01-01
    相关资源
    最近更新 更多