在 Java 中模拟 URL [英] Mocking a URL in Java

查看:21
本文介绍了在 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆