簡體   English   中英

如何使用 EasyMock 模擬 HttpUrlConnection.getResponseCode() 方法

[英]How can I mock the HttpUrlConnection.getResponseCode() method using EasyMock

我正在嘗試為下面的代碼片段編寫測試用例。 但是模擬沒有按預期工作。

Map<String, String> request = new HAshMap<String, String>();
URL url = new URL("sampleurl");
HttpUrlConnection connection = (HttpUrlConnection) url.openConnection();

if(connection != null){
connection.setDOutput(true);

OutputStreamReader out = new OutputStreamReader(connection.getOutputStream());
out.write(new Gson().toJson(request);
out.close();

int responseCode = connection.getResponseCode();
}

我已經為 mocking 嘗試了以下測試代碼片段,但 Mock 無法正常工作,我做錯了

 URL url = PowerMock.createNiceMock(URL.class);
 HttpsUrlConnection httpConn = PowerMock.createNiceMock(HttpsUrlConnection.class);

 EasyMock.expect(url.openConnection()).andReturn(httpConn);
 EasyMock.expect(httpConn.getResponseCode()).andReturn(500);

評論是對的。 需要注入一個模擬。 我強烈建議您首先記錄自己的概念。

在您的情況下,對象是在方法中創建的,因此通常我們會說它不能被模擬。 從關注點分離來看,這種方法做的事情太多了。

如果我們想保持相同的代碼和 go 的 PowerMock 方式,它看起來像這樣。 我會給你我測試過的 class 和我的測試。

public class MyClass {

    public int call() throws Exception {
        Map<String, String> request = new HashMap<>();
        URL url = new URL("sampleurl");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        return connection.getResponseCode();
    }
}

@RunWith(PowerMockRunner.class)
@PrepareForTest({MyClass.class, URL.class})
public class MyClassTest {

    private MyClass testClass = new MyClass();

    @Test
    public void test() throws Exception {
        URL url = PowerMock.createMock(URL.class);
        HttpURLConnection httpConn = PowerMock.createMock(HttpURLConnection.class);

        PowerMock.expectNew(URL.class, "sampleurl").andReturn(url);
        EasyMock.expect(url.openConnection()).andReturn(httpConn);
        EasyMock.expect(httpConn.getResponseCode()).andReturn(500);

        PowerMock.replayAll();

        assertThat(testClass.call()).isEqualTo(500);
    }
}

但我實際上永遠不會編寫這樣的代碼。 我會用更好的設計重構。 可能有另一個 class 來創建連接。 如果我正在重構,最低限度是:

public class MyClass {

    public int call() throws Exception {
        Map<String, String> request = new HashMap<>();
        HttpURLConnection connection = getConnection();
        return connection.getResponseCode();
    }

    HttpURLConnection getConnection() throws IOException {
        URL url = new URL("sampleurl");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        return connection;
    }
}

public class MyOtherTest extends EasyMockSupport {

    private MyClass testClass = partialMockBuilder(MyClass.class)
            .addMockedMethod("getConnection")
            .mock();

    @Test
    public void test() throws Exception {
        HttpURLConnection httpConn = mock(HttpURLConnection.class);
        expect(httpConn.getResponseCode()).andReturn(500);
        expect(testClass.getConnection()).andReturn(httpConn);

        replayAll();

        assertThat(testClass.call()).isEqualTo(500);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM