I am using Java nio API and have a below code, but unable to perform mocking on the below code.
Path path = Paths.get(filePath);
long sizeInBytes = Files.size(path);
long recordCount = 0;
try (Stream<String> lines = Files.lines(path, Charset.defaultCharset()).skip(1)) {
recordCount = lines.count();
}
Code I wrote below is
public class SomeTest {
private Path path;
@ClassRule
public static final TemporaryFolder testdata = new TemporaryFolder();
@Before
public void setup() throws IOException {
PowerMockito.mockStatic(Paths.class);
PowerMockito.mockStatic(Files.class);
path = mock(Path.class);
}
@Test
public void testAbRequestAuditTasklet() throws Exception {
final Path path = testdata.newFile().toPath();
Answer<Stream<String>> answer = new Answer<Stream<String>>() {
public Stream<String> answer(InvocationOnMock invocation) throws Throwable {
return Stream.of("A", "B");
}
};
when(Paths.get(testdata.getRoot().toURI())).thenReturn(path); //line-24 - here exception is coming
when(Files.size(any())).thenReturn(5000L);
when(Files.lines(any(), any())).thenAnswer(answer);
}
}