在 Java 中模拟 URL

2022-01-08 00:00:00 mocking url java

在我们想要模拟的一个 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.

相关文章