亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 restTemplate 上進行 junit 測試?

如何在 restTemplate 上進行 junit 測試?

慕斯王 2023-05-10 15:15:01
我在用 Mockito 模擬 restTemplate 時遇到問題代碼要測試:public class Feature{ public static String getfeature(String url){     RestTemplate restTemplate = new RestTemplate();     String xml = "\"feature\": 1";     String json = restTemplate.postForObject(url, xml, String.class);     return json;}}聯合代碼:@MockRestTemplate restTemplate=mock(RestTemplate.class);@Testpublic void testGetfeature(){string testResponse= "\"feature\": 1";Mockito.when((String)restTemplate.postForObject(                Mockito.any(String.class),                Mockito.any(Map.class),                Mockito.any(Class.class)                )).thenReturn(testResponse);Feature feature = new Feature();feature.getfeature("http://mockValue");}我在 feature.getfeature(" http://mockValue ")設置斷點。它仍然嘗試連接到遠程服務器。我不希望 postForObject 連接到http://mockValue。我應該如何模擬 restTemplate 使 postForObject 不連接到http://mockValue?
查看完整描述

4 回答

?
蕭十郎

TA貢獻1815條經驗 獲得超13個贊

您正在方法中創建一個新RestTemplate對象getfeature()。所以,嘲笑RestTemplate沒有效果。要么將RestTemplate其作為方法中的參數getfeature(),要么將其作為Feature類中的構造函數參數。


然后從測試類中,您可以模擬 RestTemplate 并像下面這樣傳遞它:


Feature feature= new Feature(mockRestTemplate);

feature.getfeature(url);

或者


Feature feature = new Feature();

feature.getfeature(mockRestTemplate, url);

您必須根據決定對要素類進行必要的更改。


這是運行代碼示例:


主要類:


public class Feature {

? ? public static String getFeature(String url, RestTemplate restTemplate) {

? ? ? ? return restTemplate.postForObject(url, "", String.class);

? ? }

}

測試類:


請注意模擬的方式RestTemplate,然后模擬響應。


public class FeatureTest {

? ? @Test

? ? public void testFeature() {

? ? ? ? RestTemplate restTemplate = Mockito.mock(RestTemplate.class);

? ? ? ? Mockito.when(restTemplate.postForObject(Mockito.any(String.class),

? ? ? ? ? ? ? ? Mockito.any(Object.class), Mockito.any(Class.class))).thenReturn("abc");

? ? ? ? System.out.println(Feature.getFeature("http://abc", restTemplate));

? ? }

}

運行代碼示例也可以在github上找到

Feature.java和FeatureTest.java


查看完整回答
反對 回復 2023-05-10
?
慕尼黑的夜晚無繁華

TA貢獻1864條經驗 獲得超6個贊

如何在 restTemplate 上進行 junit 測試?


我們測試它返回的內容。

目前你的實現本身什么都不做,它只是委托給 aRestTemplate并返回它的結果。


答案fiveelements描述了實現(以廣泛接受的值作為參數):


@Test 

public void testFeature() {

    RestTemplate restTemplate = Mockito.mock(RestTemplate.class);

    Mockito.when(restTemplate.postForObject(Mockito.any(String.class),

            Mockito.any(Object.class), Mockito.any(Class.class))).thenReturn("abc");

    System.out.println(Feature.getFeature("http://abc", restTemplate));

}

這并沒有斷言實際行為:URL 可能是錯誤的,發布的正文或響應可能是錯誤的等等......而這個測試永遠不會檢測到這一點。最后,這個實現中可能有非常重要的事情是錯誤的,我們無法檢測到。這種考驗的價值,實在是太弱了。

由于此方法本身不執行邏輯,因此它更適合于可以在實際行為中斷言和捕獲更多事物/問題的集成測試:


@Test 

public void testFeature() {

    String actualFeature = Feature.getFeature("http://...");

    String expectedFeature = ...;

    Assertions.assertEquals(expectedFeature, actualFeature);

}


查看完整回答
反對 回復 2023-05-10
?
MYYA

TA貢獻1868條經驗 獲得超4個贊

我認為您需要更改代碼以使您的單元測試至少使用 Mockito 工作,或者您必須使用其他一些庫(如 powermock)來模擬本地對象實例化。


1)創建一個構造函數,它接受 RestTemplate 作為參數來注入你的模擬。


或者


2) 創建一些 setter 方法來注入該 RestTemplate。


另一種方法是創建另一個可以傳遞 RestTemplate 的方法。


public String getStringAsJson(RestTemplate restTemplate, String url, String xml) {

     return  restTemplate.postForObject(url, xml, String.class);

}

然后在你的測試中:


RestTemplate mockRestTemplate = mock(RestTemplate.class);

when(restTemplate.postForObject(mockurl, mockUrl, mockXml)).thenReturn("Json");

feature.getStringAsJson(mockRestTemplate,mockUrl,mockXml);


查看完整回答
反對 回復 2023-05-10
?
桃花長相依

TA貢獻1860條經驗 獲得超8個贊

如果您使用PowerMockito,那么您可以執行以下操作:


Feature 類無需更改代碼,如果 prj 中有 PowerMockito lib,則可以直接使用它


RestTemplate mockedRestTemplate = PowerMockito.mock(RestTemplate.class);

PowerMockito.whenNew(RestTemplate.class).withAnyArguments().thenReturn(mockedRestTemplate);



查看完整回答
反對 回復 2023-05-10
  • 4 回答
  • 0 關注
  • 231 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號