【问题标题】:Unit test for Spring MVC Controllers that use annotation @RequestParam使用注解 @RequestParam 的 Spring MVC 控制器的单元测试
【发布时间】:2010-11-05 18:50:38
【问题描述】:

如何为使用注解 @RequestParam 的 Spring MVC 控制器创建单元测试?我已经为在 handlerequest 方法中使用 HttpServletRequest 对象的控制器创建了 junit 测试,但我正在寻找一种使用 @RequestParam 测试控制器的方法。

谢谢

@RequestMapping("/call.action")

public ModelAndView getDBRecords(@RequestParam("id") String id) {

   Employee employee = service.retrieveEmployee(id);

} 

【问题讨论】:

标签: unit-testing spring spring-mvc


【解决方案1】:

这种控制器风格的魅力之一是您的单元测试不需要担心请求映射的机制。他们可以直接测试目标代码,无需处理请求和响应对象。

因此,像编写任何其他类一样编写单元测试,并忽略注释。换句话说,从您的测试中调用getDBRecords() 并传入id 参数。请记住,您不需要对 Spring 本身进行单元测试,您可以假设它有效。

还有另一类测试(“功能”或“验收”测试)在应用程序部署后对其进行测试(例如使用 WebDriver、Selenium、HtmlUnit 等)。 这里是测试您的映射注释是否正常工作的地方。

【讨论】:

    【解决方案2】:

    或者,您可以使用 _request = new MockHttpServletRequest();

    和_request.setAttribute("key", "value");

    【讨论】:

      【解决方案3】:

      使用集成测试(google Spring MVC集成测试)

      有点像

      import org.junit.Assert;
      import org.junit.Test;
      import org.junit.runner.RunWith;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.beans.factory.annotation.Value;
      import org.springframework.boot.test.IntegrationTest;
      import org.springframework.boot.test.SpringApplicationContextLoader;
      import org.springframework.http.HttpStatus;
      import org.springframework.http.ResponseEntity;
      import org.springframework.test.context.ContextConfiguration;
      import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
      import org.springframework.test.context.web.WebAppConfiguration;
      import org.springframework.web.client.RestTemplate;
      import org.springframework.web.context.WebApplicationContext;
      
      @RunWith(SpringJUnit4ClassRunner.class)
      @ContextConfiguration(classes = YourApplication.class, loader = SpringApplicationContextLoader.class)
      @WebAppConfiguration
      @IntegrationTest("server.port:0")
      public class SampleControllerTest {
      
          @Value("${local.server.port}")
          protected int port;
      
          @Autowired
          protected WebApplicationContext context;
      
          private RestTemplate restTemplate = new RestTemplate();
      
          @Test
          public void returnsValueFromDb() {
              // you should run mock db before
              String id = "a0972ca1-0870-42c0-a590-be441dca696f";
              String url = "http://localhost:" + port + "/call.action?id=" + id;
      
              ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
      
              Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
      
              String body = response.getBody();
      
              // your assertions here
          }
      
      }
      

      【讨论】:

      • Lol 没想到这个问题是 5 年前提出的。无论如何,也许有人觉得这很有用
      【解决方案4】:

      试试它作为你的测试方法!

      @Test
          public void testgetDBRecords(){
            MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
            mockMvc.perform(get("/call.action?id=id1234").andExpect(status().isOk())
          }
      

      【讨论】:

        猜你喜欢
        • 2012-04-16
        • 2017-01-09
        • 1970-01-01
        • 1970-01-01
        • 2023-03-15
        • 2011-08-12
        • 1970-01-01
        • 2014-02-08
        • 1970-01-01
        相关资源
        最近更新 更多