简体   繁体   English

模拟apache.commons.io.IOUtils类

[英]Mocking apache.commons.io.IOUtils class

I need to mock toByteArray() of apache.commons.io.IOUtils class. 我需要模拟apache.commons.io.IOUtils类的toByteArray()。 I've a code snippet like ths: 我有像这样的代码片段:

PowerMockito.mockStatic(IOUtils.class);
PowerMockito.when(IOUtils.toByteArray(any(InputStream.class))).thenReturn(mockByteArray);

But I'm getting NullPointerException from org.apache.commons.io.IOUtils.copyLarge() function. 但是我从org.apache.commons.io.IOUtils.copyLarge()函数中获取了NullPointerException。

It looks like the method is not mocked and the real method is called instead. 看起来该方法没有被模拟,而是调用了real方法。

PowerMock uses special test runner that can mock static methods. PowerMock使用可以模拟静态方法的特殊测试运行器。

Put @RunWith(PowerMockRunner.class) and @PrepareForTest(IOUtils.class) annotations on your test class. @RunWith(PowerMockRunner.class)@PrepareForTest(IOUtils.class)批注放在测试类上。

See example at docs 参见文档示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android等效于org.apache.commons.io.IOUtils.toByteArray - Android equivalent for org.apache.commons.io.IOUtils.toByteArray 我应该关闭org.apache.commons.io.IOUtils的InputStream吗? - Should I close the InputStream of org.apache.commons.io.IOUtils 使用Apache commons-io IOUtils.close可以安全吗? - Is it safe to use Apache commons-io IOUtils.closeQuietly? WebLogic-Jar存在,但NoClassDefFoundError:org / apache / commons / io / IOUtils - WebLogic - Jar is there but NoClassDefFoundError: org/apache/commons/io/IOUtils 运行 maven 项目时出错 java.lang.ClassNotFoundException: org.apache.commons.io.IOUtils - error while running maven project java.lang.ClassNotFoundException: org.apache.commons.io.IOUtils java.lang.NoSuchMethodError:org.apache.commons.io.IOUtils.read with Tika(检测方法) - java.lang.NoSuchMethodError: org.apache.commons.io.IOUtils.read with Tika (detect method) 为什么导入org.apache.commons.io.IOUtils后还是报错? 如何解决它们以及如何正确添加导入? - Why there is still error after i import the org.apache.commons.io.IOUtils? How to resolve them and how to add the import properly? 模拟 Apache Commons CSV CSVRecord - Mocking an Apache Commons CSV CSVRecord Android上的Apache Commons IO - Apache Commons IO on Android Commons IO(Apache)copyURLToFile不起作用 - Commons IO (Apache) copyURLToFile Not Working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM