问题:我写一个测试用例的方法从一个公共静态方法与下面的代码被称为:如何嘲笑“新FileOutputStram()”编写公共静态方法使用Powermockito
final File file = new File(filePath);
final OutputStream out = new FileOutputStream(file);
out.write(bytes);
out.close();
现在我需要模拟上面的调用。
我所写的: -
@Before
public void setUp() throws Exception{
File myFile = PowerMockito.mock(File.class);
FileOutputStream outStream = PowerMockito.mock(FileOutputStream.class);
PowerMockito.whenNew(File.class).withAnyArguments().thenReturn(myFile);
PowerMockito.whenNew(FileOutputStream.class).withAnyArguments().thenReturn(outStream);
doNothing().when(outStream).write(Matchers.any());
doNothing().when(outStream).close();
}
@Test
public void testMethod(){
PowerMockito.mockStatic(StaticClassUtil.class);
PowerMockito.when(StaticClassUtil.uploadFile(file.getBytes(), "dummy","dummy","dummy", null)).thenReturn("dummy");
}
在调试,我发现没有模拟对象在行:
final File file = new File(filePath);
请建议在那里我得到错误的。
很高兴阅读您的评论,这对我来说是一次学习体验,并且有一天我也努力推动自己也达到黄金照明徽章。:)你和我之间的 –
:鉴于Illuminiator徽章是其中之一非常罕见的......它非常容易获得。您只需回答很多问题,并且每次在答案中获得第一个赞成票时,就可以编辑问题以改进它。换句话说:这个徽章只是关于许多答案和编辑。工作和纪律。还有其他徽章更难**。例如,有人问了100个有+1票或更多票的问题。虽然我问了+60个问题......我只有30个左右的upvotes。从这个意义上说:当你对徽章感兴趣... – GhostCat
努力工作以达到“审核者”的特权...然后你可以获得大量的徽章...只是通过工作/纪律。 – GhostCat