第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何在 restTemplate 上進(jìn)行 junit 測(cè)試?

如何在 restTemplate 上進(jìn)行 junit 測(cè)試?

慕斯王 2023-05-10 15:15:01
我在用 Mockito 模擬 restTemplate 時(shí)遇到問(wèn)題代碼要測(cè)試: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;}}聯(lián)合代碼:@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 ")設(shè)置斷點(diǎn)。它仍然嘗試連接到遠(yuǎn)程服務(wù)器。我不希望 postForObject 連接到http://mockValue。我應(yīng)該如何模擬 restTemplate 使 postForObject 不連接到http://mockValue?
查看完整描述

4 回答

?
蕭十郎

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超13個(gè)贊

您正在方法中創(chuàng)建一個(gè)新RestTemplate對(duì)象getfeature()。所以,嘲笑RestTemplate沒(méi)有效果。要么將RestTemplate其作為方法中的參數(shù)getfeature(),要么將其作為Feature類(lèi)中的構(gòu)造函數(shù)參數(shù)。


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


Feature feature= new Feature(mockRestTemplate);

feature.getfeature(url);

或者


Feature feature = new Feature();

feature.getfeature(mockRestTemplate, url);

您必須根據(jù)決定對(duì)要素類(lèi)進(jìn)行必要的更改。


這是運(yùn)行代碼示例:


主要類(lèi):


public class Feature {

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

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

? ? }

}

測(cè)試類(lèi):


請(qǐng)注意模擬的方式RestTemplate,然后模擬響應(yīng)。


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));

? ? }

}

運(yùn)行代碼示例也可以在github上找到

Feature.java和FeatureTest.java


查看完整回答
反對(duì) 回復(fù) 2023-05-10
?
慕尼黑的夜晚無(wú)繁華

TA貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超6個(gè)贊

如何在 restTemplate 上進(jìn)行 junit 測(cè)試?


我們測(cè)試它返回的內(nèi)容。

目前你的實(shí)現(xiàn)本身什么都不做,它只是委托給 aRestTemplate并返回它的結(jié)果。


答案fiveelements描述了實(shí)現(xiàn)(以廣泛接受的值作為參數(shù)):


@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));

}

這并沒(méi)有斷言實(shí)際行為:URL 可能是錯(cuò)誤的,發(fā)布的正文或響應(yīng)可能是錯(cuò)誤的等等......而這個(gè)測(cè)試永遠(yuǎn)不會(huì)檢測(cè)到這一點(diǎn)。最后,這個(gè)實(shí)現(xiàn)中可能有非常重要的事情是錯(cuò)誤的,我們無(wú)法檢測(cè)到。這種考驗(yàn)的價(jià)值,實(shí)在是太弱了。

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


@Test 

public void testFeature() {

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

    String expectedFeature = ...;

    Assertions.assertEquals(expectedFeature, actualFeature);

}


查看完整回答
反對(duì) 回復(fù) 2023-05-10
?
MYYA

TA貢獻(xiàn)1868條經(jīng)驗(yàn) 獲得超4個(gè)贊

我認(rèn)為您需要更改代碼以使您的單元測(cè)試至少使用 Mockito 工作,或者您必須使用其他一些庫(kù)(如 powermock)來(lái)模擬本地對(duì)象實(shí)例化。


1)創(chuàng)建一個(gè)構(gòu)造函數(shù),它接受 RestTemplate 作為參數(shù)來(lái)注入你的模擬。


或者


2) 創(chuàng)建一些 setter 方法來(lái)注入該 RestTemplate。


另一種方法是創(chuàng)建另一個(gè)可以傳遞 RestTemplate 的方法。


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

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

}

然后在你的測(cè)試中:


RestTemplate mockRestTemplate = mock(RestTemplate.class);

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

feature.getStringAsJson(mockRestTemplate,mockUrl,mockXml);


查看完整回答
反對(duì) 回復(fù) 2023-05-10
?
桃花長(zhǎng)相依

TA貢獻(xiàn)1860條經(jīng)驗(yàn) 獲得超8個(gè)贊

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


Feature 類(lèi)無(wú)需更改代碼,如果 prj 中有 PowerMockito lib,則可以直接使用它


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

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



查看完整回答
反對(duì) 回復(fù) 2023-05-10
  • 4 回答
  • 0 關(guān)注
  • 225 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)