2012-04-08 159 views
1

我有一個serveresource方法,用於ajax調用。我正在嘗試使用MockResourceRequest和MockResourceResponse爲此serverresource方法編寫單元測試。如果有人有這樣的例子,我真的需要一些幫助。在此先感謝..使用mockresourcerequest進行單元測試

請參閱下面的控制器來處理Ajax調用這是JSR發球資源的方法286

[編輯]

@Controller 
@RequestMapping("VIEW") 
public class Controller { 


@ResourceMapping(value = "") 
public String addMethod(ResourceRequest request, ResourceResponse response){ 
     /* My logic to send the request attributes to Web Service and then push 
     the results to the JSP page*/ 
    } 
} 
+0

請張貼一些代碼。你試圖測試的方法的特徵是什麼?你怎麼一直在試圖測試它,爲什麼你的努力沒有成功 - 測試沒有運行,你會得到一個例外,什麼? – ZeroOne 2012-04-22 21:44:21

+0

@ZeroOne請參閱上面的我的班級簽名和方法。 ResourceMapping是我用作資源url的值,並將數據發送到上面定義的控制器來處理。我真的很困惑如何編寫這個控制器的單元測試。 – user525146 2012-04-23 18:51:40

回答

0
@Autowired 
Controller controller; 

@Test 
public void addMethod(){ 
    MockResourceResponse response = new MockResourceResponse(); 
    MockResourceRequest request = new MockResourceRequest(); 
    controller.addMethod(requesr,response); 
    assertThat(response.getContentAsString(), equalTo("my xml content")); 
} 

或更復雜這裏UNIT TESTING SPRINGMVC PORTLETS