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

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

如何模擬 HttpClientBuilder 進(jìn)行單元測試

如何模擬 HttpClientBuilder 進(jìn)行單元測試

精慕HU 2023-08-04 15:49:22
我正在嘗試為 http post 實現(xiàn)編寫一個單元測試。但是,我無法正確模擬 httpclient,并且我的 when 語句永遠(yuǎn)不會被觸發(fā)。我編寫的單元測試是進(jìn)行實際的 http 調(diào)用,而不是使用模擬響應(yīng)進(jìn)行響應(yīng)。我們?nèi)绾卫^續(xù)模擬 HttpClientBuilder 創(chuàng)建的客戶端?Http方法實現(xiàn):HttpResponse postRequest(String url, String request) {    HttpResponse resp = null;    try {        HttpClient client = HttpClientBuilder.create().useSystemProperties().build();        HttpPost post = new HttpPost(url);        post.addHeader("Content-Type", "application/x-www-form-urlencoded");        post.setEntity(new StringEntity(request));        resp = client.execute(post);    } catch (Exception e) {        return null;    }}測試方法:@Mockprivate HttpClient httpClient;when(httpClient.execute(any())).thenReturn(httpResponse);
查看完整描述

1 回答

?
飲歌長嘯

TA貢獻(xiàn)1951條經(jīng)驗 獲得超3個贊

我們?nèi)绾卫^續(xù)模擬 HttpClientBuilder 創(chuàng)建的客戶端?


我們不!


盡量避免嘲笑第三方的擔(dān)憂


創(chuàng)建緊密耦合的靜態(tài)實現(xiàn)問題的抽象


public interface HttpClientFactory {

    public HttpClient create();

}

通過一個將在生產(chǎn)中使用的簡單實現(xiàn)。


public class HttpClientFactoryImpl implements HttpClientFactory {


    //...


    public HttpClient create() {

        return HttpClientBuilder.create().useSystemProperties().build();

    }


    //...

}

使用依賴倒置,封裝類應(yīng)該顯式依賴于抽象,以避免違反單一職責(zé)原則(SRP)


public class SystemUnderTest {


    private HttpClientFactory httpClientFactory;


    public SystemUnderTest(HttpClientFactory httpClientFactory) {

        this.httpClientFactory = httpClientFactory;

    }


    HttpResponse postRequest(String url, String request) {

        HttpResponse resp = null;

        try {

            HttpClient client = httpClientFactory.create();

            HttpPost post = new HttpPost(url);

            post.addHeader("Content-Type", "application/x-www-form-urlencoded");

            post.setEntity(new StringEntity(request));


            resp = client.execute(post);

            return resp;

        } catch (Exception e) {

            return null;

        }

    }

}

這種關(guān)注點分離 (SoC) 使其(您的封裝類)能夠更靈活地進(jìn)行單獨的單元測試。


@Test

public void testPostRequest() throws Exception {

    // Arrange

    HttpResponse expected = mock(HttpResponse.class);

    HttpClient httpClient = mock(HttpClient.class);

    when(httpClient.execute(any())).thenReturn(expected);


    HttpClientFactory httpClientFactory = mock(HttpClientFactory.class);

    when(httpClientFactory.create()).thenReturn(httpClient);


    SystemUnderTest systemUnderTest = new SystemUnderTest(httpClientFactory);


    String url = "http://url_here";

    String request = "Hello World";


    // Act

    HttpResponse actual = systemUnderTest.postRequest(url, request);


    // Assert

    assertEquals(expected, actual);

    //should also verify that the expected arguments as passed to execute()

}


查看完整回答
反對 回復(fù) 2023-08-04
  • 1 回答
  • 0 關(guān)注
  • 161 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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