在 Java 中模拟 URL

Mocking a URL in Java(在 Java 中模拟 URL)
本文介绍了在 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Show progress during FTP file upload in a java applet(在 Java 小程序中显示 FTP 文件上传期间的进度)
How to copy a file on the FTP server to a directory on the same server in Java?(java - 如何将FTP服务器上的文件复制到Java中同一服务器上的目录?)
FTP zip upload is corrupted sometimes(FTP zip 上传有时会损坏)
Enable logging in Apache Commons Net for FTP protocol(在 Apache Commons Net 中为 FTP 协议启用日志记录)
Checking file existence on FTP server(检查 FTP 服务器上的文件是否存在)
FtpClient storeFile always return False(FtpClient storeFile 总是返回 False)