单元测试期间发生NullPointerException

时间:2019-04-18 10:43:56

标签: java unit-testing junit mockito

我正在尝试对覆盖文件的覆盖方法进行单元测试(如果给定的文件不存在,则创建一个新的覆盖方法)。 我的想法是在覆盖之前和覆盖之后与数组进行比较,所以我做到了,但是不幸的是(一如既往)发生了NPE。

这是我要测试的类的方法:

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();
        }
    }
}

这是ContentFileRetriever类:

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);
    }
}

这是测试:

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);
    }
}

这是堆栈跟踪:

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)

我在做什么错了?

1 个答案:

答案 0 :(得分:2)

我在评论人员的帮助下解决了这个问题。 需要添加:

@Before
public void setup() {
    MockitoAnnotations.initMocks(this);
    rankingSystemService = new RankingSystemService(contentFileRetrieverService);
}