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

09-05 12:32