I am trying to unit test method that is responsible for retreiving data from text file.
Here is how the class with that method looks like:
package contentfile;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;
public class ContentFileRetrieverService implements ContentFileRetriever {
@Override
public String[] getContentFile(String pathName) {
Stream<String> contentFileStream;
try {
contentFileStream = Files.lines(Paths.get(pathName));
} catch (IOException e) {
throw new IllegalArgumentException(e);
}
return contentFileStream.toArray(String[]::new);
}
}
Here is how the test looks like:
package contentfile;
import org.junit.Rule;
import org.junit.jupiter.api.Test;
import static org.junit.Assert.*;
class ContentFileRetrieverServiceTest {
private ContentFileRetrieverService contentFileRetrieverService;
// @Rule
// TemporaryFiles temporaryFiles = new TemporaryFiles();
@Test
void getContentFile() {
String pathFile = "tekst.txt";
String[] testedContent = contentFileRetrieverService.getContentFile(pathFile);
String[] expected = {"la", "la"};
assertArrayEquals(expected, testedContent);
}
}
Unfortunately, I got NullPointer
while calling getContentFile method.
Here is stacktrace:
java.lang.NullPointerException
at contentfile.ContentFileRetrieverServiceTest.getContentFile(ContentFileRetrieverServiceTest.java:18)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
Content file
Line1 a
Line2 b c
Line 3