[英]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)
批注放在测试类上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.