2012-04-03 36 views
1

我將單元測試一個Spring MVC控制器(如果可能的話)或整個站點。Spring MVC單元測試 - 我可以傳遞URL(帶參數)給控制器嗎?

我想傳遞一個URL(字符串),例如「/度量/過濾器?A1 = 1 & A2 = 2 & A3 = ABCDEFG & wrongParam = WRONG」 和檢查什麼控制器將返回。

有沒有簡單的方法來做到這一點?

例子:

@RequestMapping(value = {"/filter"}, method = RequestMethod.GET) 
    @ResponseBody 
    public List<MetricType> getMetricTypes(
      @RequestParam(value = "subject", required = false) Long subjectId 
      ,@RequestParam(value = "area", required = false) Long areaId 
      ,@RequestParam(value = "onlyImmediateChildren", required = false) Boolean onlyImmediateChildren 

      ,@RequestParam(value = "componentGroup", required = false) Long componentGroupId 

      ,@RequestParam(value = "hasComponentGroups", required = false) Boolean hasComponentGroups 
              ) throws Exception 
    { 
      //some code 
    } 

非常感謝

馬克西姆

修訂

  • 我只使用GET,不得發佈
  • 我不使用模型對象(見例以上)
  • 我的系統是具有大量的web服務「/ someEntity /過濾?A1 = 123 & A2 = 1234 & A3 =等」方法用的參數的各種組合。我不確定在這種情況下使用模型對象是否可行。

回答

3
@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration("file:WebRoot/WEB-INF/path/to/your-context.xml") 
public class YourControllerTest { 

    private MockHttpServletRequest request; 
    private MockHttpServletResponse response; 
    private AnnotationMethodHandlerAdapter adapter; 


@Before 
public void setUp(){ 
    this.request = new MockHttpServletRequest(); 
    this.response = new MockHttpServletResponse(); 
    this.adapter = new AnnotationMethodHandlerAdapter(); 
} 


    @Test 
    public void getMetricTypes() throws Exception{ 



     request.setRequestURI("/filter"); 
     request.setMethod("GET"); 
     request.setParameter("subject", "subject"); 
     request.setParameter("area", "area");  
     request.setParameter("onlyImmediateChildren", "onlyImmediateChildren");  
     request.setParameter("componentGroup", "componentGroup");  
     request.setParameter("hasComponentGroups", "hasComponentGroups");  

     ModelAndView mav = adapter.handle(request, response, yourController); 
     Assert.assertEquals(200, response.getStatus()); 
     //Assert what you want 
    } 
} 
+0

看起來很有趣。很抱歉在Spring MVC中成爲新手,但是如何獲取/創建請求和響應對象?我將你的代碼粘貼到IDE並且不能編譯。 – 2012-04-04 11:32:51

+0

我已經初始化了請求和響應,如下所示:「MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse();」。現在我無法運行測試 - 它會因NullPointerException而失敗。 – 2012-04-04 12:09:25

+0

我更新了代碼,請參閱設置方法 – 2012-04-04 13:31:56

0

大多數人不推薦集成測試請求映射 - 你可以單元測試你的控制器pojo方法:即沒有任何彈簧綁定。如果Spring在工作,那麼測試的意義是什麼?

所以在你的單元測試調用控制器方法通常在模型的實現傳遞作爲參數(extendedmodelmap(?),測試代碼,然後可以檢查哪些被添加到模型中,並返回值。

如果你真的想集成測試彈簧MVC this article is good.

+0

「大多數人建議不要集成測試請求映射」 - 我嘗試過單元測試,他們似乎對我的方法來說太重了。我想嘗試使用字符串URL(包括查詢字符串)的集成測試方法。 – 2012-04-03 15:12:14

+0

@MaximEliseev我曾經認爲,那麼我按照我的佈局測試方式進行了測試 - 它更簡單,更輕量級 - 您不需要加載應用程序上下文。我徹底推薦它。它爲什麼首先創造了彈簧(以及它的主要優點之一 - 更簡單的測試,更少的綁定)。 – NimChimpsky 2012-04-03 15:13:24

+0

謝謝你的評論。我試圖爲這些控制器編寫單元測試,但不是很快。我將會進行很多類似的測試,檢查有效的參數組合。我想在一個帶有URL的CSV文件中指定我的測試(例如「/ metricTypes/filter?area = 123&wrongParam = Bad」)和一些布爾列, IsException Expected),然後使用通用代碼從這些CSV文件運行測試。這將增加新的測試10秒的工作。 – 2012-04-04 12:13:59

0

測試Spring MVC控制器一種新的,更簡單的方法是spring-test-mvc

相關問題