我有以下代码:
Map<String, String> fileContentsByName = new HashMap<String, String>();
try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(directory))
{
for (Path path : directoryStream)
{
if (Files.isRegularFile(path))
{
fileContentsByName.put(path.getFileName().toString(), new String(Files.readAllBytes(path)));
}
}
}
catch (IOException e)
{
}我正在尝试测试这个方法。我正在使用Powermock来获取模拟的DirectoryStream<Path>。但是,当测试遇到代码中的-每一个时,它会与NPE一起爆炸。如何在DirectoryStream中指定路径?
我考虑过修改源代码以使用迭代器,并模拟DirectoryStream的迭代器以提供所需的路径,但我想知道是否有更好的选择?
发布于 2017-11-03 21:26:41
假设上面提供的代码片段是在如下类中定义的:
public class DirectoryStreamReader {
public Map<String, String> read(Path directory) {
Map<String, String> fileContentsByName = new HashMap<String, String>();
try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(directory)) {
for (Path path : directoryStream) {
if (Files.isRegularFile(path)) {
fileContentsByName.put(path.getFileName().toString(), new String(Files.readAllBytes(path)));
}
}
} catch (IOException e) {
}
return fileContentsByName;
}
}然后,以下测试将通过:
@RunWith(PowerMockRunner.class)
@PrepareForTest({DirectoryStreamReader.class})
public class DirectoryStreamTest {
@Rule
public TemporaryFolder folder= new TemporaryFolder();
@Test
public void canReadFilesUsingDirectoryStream() throws IOException {
PowerMockito.mockStatic(Files.class);
Path directory = Mockito.mock(Path.class);
DirectoryStream<Path> expected = Mockito.mock(DirectoryStream.class);
Mockito.when(Files.newDirectoryStream(Mockito.any(Path.class))).thenReturn(expected);
File fileOne = folder.newFile();
File fileTwo = folder.newFile();
Iterator<Path> directoryIterator = Lists.newArrayList(Paths.get(fileOne.toURI()),
Paths.get(fileTwo.toURI())).iterator();
Mockito.when(expected.iterator()).thenReturn(directoryIterator);
Mockito.when(Files.isRegularFile(Mockito.any(Path.class))).thenReturn(true);
Mockito.when(Files.readAllBytes(Mockito.any(Path.class))).thenReturn("fileOneContents".getBytes()).thenReturn("fileTwoContents".getBytes());
Map<String, String> fileContentsByName = new DirectoryStreamReader().read(directory);
Assert.assertEquals(2, fileContentsByName.size());
Assert.assertTrue(fileContentsByName.containsKey(fileOne.getName()));
Assert.assertEquals("fileOneContents", fileContentsByName.get(fileOne.getName()));
Assert.assertTrue(fileContentsByName.containsKey(fileTwo.getName()));
Assert.assertEquals("fileTwoContents", fileContentsByName.get(fileTwo.getName()));
}
}这里的要点是:
TemporaryFolder规则创建和丢弃一些供测试使用的文件java.nio.file.Files的所有交互,这是最后一个类,所模拟的方法是静态的,因此需要使用PowerMockito@RunWith(PowerMockRunner.class)注释。@PrepareForTest({ClassThatCallsTheSystemClass.class})注释。mockStatic(SystemClass.class)模拟系统类
https://stackoverflow.com/questions/47101232
复制相似问题