gpt4 book ai didi

java - 模拟 HTTPSURLConnection 类抛出 java.lang.AbstractMethodError

转载 作者:行者123 更新时间:2023-12-02 13:25:11 25 4
gpt4 key购买 nike

我想模拟以下几行

static void processRemoteToLocal(String srcUrl, String destFile) {

URL fileUrl = new URL(srcUrl);

HttpsURLConnection.setDefaultSSLSocketFactory(Foo.getSslContext().getSocketFactory());

HttpsURLConnection.setDefaultHostnameVerifier(Foo.getHostnameVerifier());

HttpsURLConnection connection = (HttpsURLConnection) fileUrl.openConnection();

}

对于上面的代码,我使用 PowerMockito 更新了我的测试类,如下所示

@Test
@PrepareForTest({Foo.class,SSLSocketFactory.class})
public void shouldSetTestMockServeField() throws Exception {

HostnameVerifier hnameMock = PowerMockito.mock(HostnameVerifier.class);
SSLSocketFactory mockSocFac = PowerMockito.mock(SSLSocketFactory.class);
HttpsURLConnection huc = PowerMockito.mock(HttpsURLConnection.class);
Foo mockCert = PowerMockito.mock(Foo.class);
SSLContext sslMock = PowerMockito.mock(SSLContext.class);

final SSLSocketFactory sslFac = null;

URL u = PowerMockito.mock(URL.class);
String url = "https://localhost";
PowerMockito.whenNew(URL.class).withArguments(url).thenReturn(u);

PowerMockito.mockStatic(Foo.class);
Mockito.when(Foo.getSslContext()).thenReturn(sslMock);
Mockito.when(sslMock.getSocketFactory()).thenReturn(mockSocFac);

这会在最后一行出现 java.lang.NullPointerException 错误。

有人可以建议如何修复它吗?

最佳答案

我使用 java 7、JUnit 4.12、Mockito 1.10.19 和 PowerMock 1.6.3 进行了测试

我用你想要模拟的代码创建了一个类:

public class HttpTest {

static void processRemoteToLocal(String srcUrl, String destFile) throws Exception {
URL fileUrl = new URL(srcUrl);

HttpsURLConnection.setDefaultSSLSocketFactory(Foo.getSslContext().getSocketFactory());

HttpsURLConnection.setDefaultHostnameVerifier(Foo.getHostnameVerifier());

HttpsURLConnection connection = (HttpsURLConnection) fileUrl.openConnection();
}
}

我还根据您上面的评论创建了类 Foo:

public class Foo {
// I'm just returning something, not sure how your implementation is (and it doesn't make difference because you'll mock it anyway)
public static SSLContext getSslContext() {
try {
return SSLContext.getDefault();
} catch (NoSuchAlgorithmException e) {
return null;
}
}

// I'm just returning something, not sure how your implementation is (and it doesn't make difference because you'll mock it anyway)
public static HostnameVerifier getHostnameVerifier() {
return new HostnameVerifier() {

@Override
public boolean verify(String arg0, SSLSession arg1) {
return true;
}
};
}
}

由于您想要模拟 processRemoteToLocal 方法中的代码,因此您只需要模拟以下内容:

  • URL 构造函数
  • Foo 方法:getSslContext()getHostnameVerifier()
  • URL.openConnection() 方法

所以你的测试类将是这样的:

// RunWith needed for powermock
@RunWith(PowerMockRunner.class)
@PrepareForTest({ HttpTest.class, Foo.class, SSLContext.class, URL.class })
public class MyTestClass {

@Test
public void shouldSetTestMockServeField() throws Exception {
URL u = PowerMockito.mock(URL.class);
String url = "https://localhost";
// mock URL constructor
PowerMockito.whenNew(URL.class).withArguments(Matchers.anyString()).thenReturn(u);
// mock openConnection() method
HttpsURLConnection huc = Mockito.mock(HttpsURLConnection.class);
Mockito.when(u.openConnection()).thenReturn(huc);

// create mocks for Foo class
HostnameVerifier hnameMock = Mockito.mock(HostnameVerifier.class);
SSLContext context = PowerMockito.mock(SSLContext.class);
SSLSocketFactory mockSocFac = Mockito.mock(SSLSocketFactory.class);
Mockito.when(context.getSocketFactory()).thenReturn(mockSocFac);

// mock Foo static methods to return mocks created above
PowerMockito.mockStatic(Foo.class);
Mockito.when(Foo.getHostnameVerifier()).thenReturn(hnameMock);
Mockito.when(Foo.getSslContext()).thenReturn(context);

// call static method, code will use mocked objects
// Foo static methods will return mocked SSLContext and HostnameVerifier created above
// URL.openConnecton will return mocked HttpsURLConnection
HttpTest.processRemoteToLocal(url, "/test.out");

// do the assertions you need
}
}

关于java - 模拟 HTTPSURLConnection 类抛出 java.lang.AbstractMethodError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43410965/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com