Im trying to unit test my overwriting method that overwrites the file (or create new one if given file does not exists). My idea was to compare to arrays before overwriting and after overwriting, so I made it, but unfortunately (as always) NPE occured.
Here is the method with the class I want to test:
package rankingsystem;
import contentfile.ContentFileRetriever;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
public class RankingSystemService {
private static final int FIRST_PART = 0;
private static final int SECOND_PART = 1;
private ContentFileRetriever contentFileRetriever;
public RankingSystemService(ContentFileRetriever contentFileRetriever) {
this.contentFileRetriever = contentFileRetriever;
}
...
String[] retrieveRankingData(String rankingPathFile) {
return contentFileRetriever.getContentFile(rankingPathFile);
}
void overwriteFileWithGivenResult(String name, long timeOfFinishingGame, String rankingPathFile) {
try (FileWriter writer = new FileWriter(rankingPathFile, true);
BufferedWriter bufferedWriter = new BufferedWriter(writer)) {
bufferedWriter.write(name + " " + timeOfFinishingGame);
bufferedWriter.newLine();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Here is the ContentFileRetriever
class:
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 the test:
package rankingsystem;
import contentfile.ContentFileRetriever;
import contentfile.ContentFileRetrieverService;
import org.junit.Test;
import org.mockito.Mock;
import java.util.LinkedHashMap;
import java.util.Map;
import static org.junit.Assert.*;
public class RankingSystemServiceTest {
@Mock
ContentFileRetrieverService contentFileRetrieverService;
private RankingSystemService rankingSystemService = new RankingSystemService(contentFileRetrieverService);
...
@Test
public void overwriteFileWithGivenResult() {
String pathFile = "src\\test\\java\\resources\\RankingFile.txt";
String[] beforeOverwriting = rankingSystemService.retrieveRankingData(pathFile);
String[] expectedResultBeforeOverwriting = {};
assertArrayEquals(expectedResultBeforeOverwriting, beforeOverwriting);
rankingSystemService.overwriteFileWithGivenResult("Piotr", 1L, pathFile);
String[] afterOverwriting = rankingSystemService.retrieveRankingData(pathFile);
String[] expectedResultAfterOverwriting = {"Piotr 1"};
assertArrayEquals(expectedResultAfterOverwriting, afterOverwriting);
}
}
Here is the stacktrace:
java.lang.NullPointerException
at rankingsystem.RankingSystemService.retrieveRankingData(RankingSystemService.java:38)
at rankingsystem.RankingSystemServiceTest.overwriteFileWithGivenResult(RankingSystemServiceTest.java:39)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
What am I making wrong?