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


- Java包名称中单词分隔符的约定是什么? 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01