- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想模拟以下几行
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/
我有一个使用 Spring 3.0.5、JPA2 和 Hibernate 3.6.7 的应用程序。 Maven 处理我的依赖管理。这是一个 pom 摘录: 3.0.5.RELEASE
我对配备 jboss 开发工作室的 Eclipse IDE (Juno RE 2) 有问题。我有几个使用 JPA 的 Maven 项目。在这些项目中,Eclipse 报错“ 此项目具有 JPA 方面,
我正在尝试使用 java 代码向 postgres 插入一个数组,但我总是收到此错误: SEVERE [http-nio-8080-exec-2]org.apache.catalina.core.St
让我首先总结一下我到底想做什么。基本上,我使用 JavaCompiler 包在运行时编译一个扩展我的父类(super class)“Player”的类。我唯一知道的是,它将在子类中扩展 Player
我的电脑中的 Web 服务程序出现错误,但相同的代码在其他电脑上运行正常。有什么解决办法吗? SEVERE: Servlet.service() for servlet [ServletAdaptor
当我在 ant 中运行 junit 测试时出现以下错误。测试类需要调用web service API。 有人可以帮忙解决吗? java.lang.AbstractMethodError: javax.
我刚刚尝试关注 this为我的 Web 服务实现基于 token 的身份验证的教程,刚刚完成了“保护 REST 端点”步骤,但现在在调用端点时我总是收到 AbstractMethodError。 我已
我正在 tomcat 8.5 中构建一个简单的 JSF 2.3 应用程序。我让 websocket 正常工作,然后添加了 Pretty Faces。 如果我单独使用它们,它们都可以工作。我的意思是,如
考虑以下代码: object Impls { trait ConcreteImpl { type Foo = Int def foo = 1 } } trait Base {
我正在部署一个在我自己的机器上运行的 war 并收到错误: java.lang.AbstractMethodError: com.mchange.v2.c3p0.impl.NewProxyPrepar
将 hibernate(在 JBoss 7.2.0 中)从 4.2.14 更新到 5.3.7 后,出现 AbstractMethodError 异常。这可能是一些依赖性问题,但我不知道应该在哪里寻找。
在尝试启动 SpringApplication 时,使用 Maven 添加 Jadira 依赖项会导致 AbstractMethodError。堆栈跟踪中没有任何帮助;它没有引用我自己的任何类(cla
我正在尝试制作一个 Elasticsearch 传输客户端,但出现以下错误 Caused by: java.lang.AbstractMethodError: org.elasticsearch.tr
我刚刚将应用程序的 GCM 更改为 FCM,并向其中添加了 OneSignal。完全遵循指南,甚至创建了一个新的监听器服务来处理推送通知。 然而,即使没有监听器,每当我收到推送时,应用程序都会崩溃并显
我在 android studio bumblebee 2021.1.1 上工作,并在创建 compose 项目时出现此错误 java.lang.AbstractMethodError: abstra
我是网络服务新手。我尝试将 jax.ws.. Web 服务与 jersey 和 spring 一起使用。当我尝试在 Windows 中使用 Chrome Post Client 或 CURL 发送请求
我正在项目中将 newrelic 版本从 2.x.x 更新到 5.x.x。我按照 newrelic 文档中提到的步骤进行操作(我更新了 pom 文件中的依赖项版本,更新了代理 jar 文件和配置文件)
我最近开始使用 Jackon 和 SmileFactory API's 来序列化 JSON 文档,但不知何故,每当我尝试使用 SmileFactory properties = new HashMap
我正在尝试从 Apache Spark 示例 mlib 推荐器 http://spark.apache.org/docs/1.2.1/mllib-collaborative-filtering.htm
我在我的项目中使用atomikos Essentials 来管理交易。部署项目后出现以下异常。我陷入其中无法解决。谁能帮我解决这个问题。 Exception in thread "Atomikos:0
我是一名优秀的程序员,十分优秀!