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

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

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

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);
添加回答
舉報(bào)