对 java 模拟文件的建议(模拟 java.io.File)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3148089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Suggestions for a java Mock File (to mock java.io.File)
提问by michel.iamit
Does anyone have suggestions for a java mock File object? I Am using a thirdparty class which need to get a java.io.File object as argument. I receive the data for this file in a stream over a webservice (also one of their products).
有没有人对 java 模拟 File 对象有建议?我正在使用需要获取 java.io.File 对象作为参数的第三方类。我通过网络服务(也是他们的产品之一)在流中接收此文件的数据。
One solution is to write all this data to a file and offer this to the class. This is a solution I don't like: it takes away the advantage of using the webservice in stead of just downloading the file.
一种解决方案是将所有这些数据写入一个文件并将其提供给类。这是我不喜欢的解决方案:它取消了使用 web 服务的优势,而不仅仅是下载文件。
Quicker and more efficient would be to put this data from memory in a Mock File and offer this Mock File to the thirdparty class.
将内存中的这些数据放在一个模拟文件中并将这个模拟文件提供给第三方类会更快、更有效。
It would probably have to be a MockFile extending the java.io.File and overriding all the functions that do actual interfacing with the file on the hard disk.
它可能必须是一个 MockFile 扩展 java.io.File 并覆盖所有与硬盘上的文件进行实际交互的函数。
I know the thirdparty should have used a stream as an input argument in stead of a file. However, this is beyond my influence.
我知道第三方应该使用流作为输入参数而不是文件。然而,这超出了我的影响。
采纳答案by chedine
This is just a suggestion based on my understanding of your question. I believe, you must be doing something like this,
这只是基于我对您的问题的理解的建议。我相信,你一定在做这样的事情,
public void doSomething(){
//Pre processing
Object result=new ThirdPartyCode().actualMethod(file);
//Post processing
}
Mock objects make more sense from an unit testing perspective. Your objective is not to unit test the third party library function.Whereas it is to unit test doSomething() method. So probably you can create a wrapper around the third party function.Maybe something like this,
从单元测试的角度来看,模拟对象更有意义。您的目标不是对第三方库函数进行单元测试,而是对 doSomething() 方法进行单元测试。所以也许你可以围绕第三方函数创建一个包装器。也许是这样的,
public class Wrapper implements MyWrapper{
public Object invokeThirdPartyFunction(File file){
new ThirdPartyCode().actualMethod(file);
}
}
Now you can create a mock wrapper(implementing the same interface) and use this mock wrapper for all your junit cases.
现在您可以创建一个模拟包装器(实现相同的接口)并为您的所有 junit 案例使用这个模拟包装器。
回答by Péter T?r?k
Does the tested class only query the mock File's name, attributes etc., or does it actually attempt to open the file?
被测试的类是只查询模拟文件的名称、属性等,还是它实际上尝试打开文件?
In the former case, you can easily create your mock using e.g. EasyMockor an equivalent mocking framework.
在前一种情况下,您可以使用EasyMock或等效的模拟框架轻松创建模拟。
The latter case is more tricky, and I am afraid if the input stream is created internally by the class, you have no choice other than actually creating a real test file on the HD.
后一种情况更棘手,我担心如果输入流是由类在内部创建的,除了在 HD 上实际创建一个真正的测试文件之外,您别无选择。
回答by Stephen Connolly
You could load the 3rd party code using an ASM based classloader that maps java.io.Fileto your own "fake" implementation. It's a bit of work, and needs to be performed carefully... For example you will need to also map FileInputStream, etc.
您可以使用映射java.io.File到您自己的“假”实现的基于 ASM 的类加载器加载第 3 方代码。这是一项工作,需要仔细执行...例如,您还需要 mapFileInputStream等。
回答by Sumit Bisht
You don't use file (or any external dependency in Unit tests). Except using mocks, your approaches will result in problematic tests. See this javaranch articlefor more
您不使用文件(或单元测试中的任何外部依赖项)。除了使用模拟之外,您的方法将导致有问题的测试。有关更多信息,请参阅此 javaranch 文章

