Mocking a URL in Java(在 Java 中模拟 URL)
问题描述
在我们想要模拟的一个 Java 类中有一个 URL 对象,但它是最终类,所以我们不能.我们不想更上一层楼并模拟 InputStream,因为这仍然会给我们留下未经测试的代码(我们有严格的测试覆盖标准).
We have a URL object in one of our Java classes that we want to mock, but it's a final class so we cannot. We do not want to go a level above, and mock the InputStream because that will still leave us with untested code (we have draconian test coverage standards).
我尝试了 jMockIt 的反射功能,但我们在 Mac 上工作,Java 代理处理程序存在我无法解决的问题.
I've tried jMockIt's reflective powers but we work on Macs and there are problems with the Java agent handler that I haven't been able to resolve.
那么有没有在junit测试中不涉及使用真实URL的解决方案?
So are there any solutions that do not involve using real URLs in the junit test?
推荐答案
当我有一个类因为它是 final (或在 C# 中密封)而不能轻易模拟时,我通常的路线是围绕类并在我使用实际类的任何地方使用包装器.然后我会根据需要模拟包装类.
When I have a class that can't be easily mocked because it is final (or sealed in C#), my usual route is to write a wrapper around the class and use the wrapper wherever I would use the actual class. Then I would mock out the wrapper class as necessary.
这篇关于在 Java 中模拟 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Java 中模拟 URL


基础教程推荐
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 降序排序:Java Map 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01