0

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
pipilam
  • 587
  • 3
  • 9
  • 22
  • I see two problems here, aside from the NPE which is solved by the answer. First, you never close the stream, which is reading from a file. Did you mean to use a try-with-resources statement? Second, your unit test is reading from an actual file. To me, it isn't a unit test if it needs to access the filesystem. I would mock out Files.lines, somehow. – David Conrad Apr 17 '19 at 21:27
  • Also, you should probably translate IOException into UncheckedIOException, not IllegalArgumentException. – David Conrad Apr 17 '19 at 21:28
  • @DavidConrad I dont know if I should close the stream or not. On EVERY site I was that was whole code to read from file, there was not any of closing the stream. `you should probably translate IOException into UncheckedIOException,` If I write `UncheckedIOException` then IDE force me to catch also IOException, so why you say that I need to translae IO to UncheckedIO? – pipilam Apr 17 '19 at 21:52
  • Then every site is wrong. The reason Stream has a close() method is because it might be operating on a resource that needs to be closed, like a file. The IDE should not do that with UncheckedIOException. – David Conrad Apr 18 '19 at 11:19
  • Just read the documentation for [`Files.lines`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/nio/file/Files.html#lines(java.nio.file.Path)). – David Conrad Apr 18 '19 at 11:23
  • @DavidConrad I dont think every site is wrong. For example https://www.mkyong.com/java8/java-8-stream-read-a-file-line-by-line/ To sum up, you say I should add `contentFileStream.close();` after try/catch block? – pipilam Apr 18 '19 at 11:49
  • That site shows using a try-with-resources block, which automatically closes the stream. You used an ordinary try block, which doesn't. – David Conrad Apr 18 '19 at 11:52
  • Which one is better? This from site or closing stream in finally block? – pipilam Apr 18 '19 at 11:54
  • They both do the same thing. Try-with-resources automatically generates the finally block for you. But you need to read the lines into the array before the stream is closed, so either read them into a local String[] variable, or move the return statement into the block. – David Conrad Apr 18 '19 at 11:57
  • Okay, I have made something like this. Is it properly written right now? https://pastebin.com/KSuWjJdV – pipilam Apr 18 '19 at 13:23

1 Answers1

5

private ContentFileRetrieverService contentFileRetrieverService; is null, hence the exception.

You'll need to instantiate this before testing it:

private ContentFileRetrieverService contentFileRetrieverService = new ContentFileRetrieverService();

StuPointerException
  • 7,117
  • 5
  • 29
  • 54