1

I have tried to look all over stackoverflow and none of the answer solves my problem. For some reason, my mocked object is invoking the actual method when when was invoked. And this is calling the test to throw NullPointerException.

The offending line is this: when(fileReader.readLines(any(Path.class))).thenReturn(lines);

Can someone tell me what I am doing wrong and How I can fix this issue?

Below are the minimally code required to run the test

FileReaderTest.java

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.mock;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.doReturn;
import static org.mockito.ArgumentMatchers.any;

public class FileReaderTest {
    @Test 
    public void mockTest() throws IOException {
        FileReader fileReader = mock(FileReader.class);
        Stream.Builder<String> stream = Stream.builder();
        stream.add("\n");
        Stream<String> lines = stream.build();
        when(fileReader.readLines(any(Path.class))).thenReturn(lines);
    }
}

FileReader.java

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.nio.charset.StandardCharsets;

public class FileReader {

    private FileReader() {}

    public static final FileReader newInstance() {
        return new FileReader();
    }

    public final Stream<String> readLines(Path filePath) throws IOException {
        System.out.println("readLines");
        final FileInputStream fileInputStream 
                = new FileInputStream(filePath.toFile()); 
        final InputStreamReader inputStreamReader 
                = new InputStreamReader(fileInputStream, StandardCharsets.UTF_8);
        final BufferedReader reader = new BufferedReader(inputStreamReader);
        return reader.lines();
    }
}

STDOUT

> Task :test                                                                                          

FileReaderTest > mockTest() STANDARD_OUT                              
    readLines                                      

FileReaderTest > mockTest() FAILED                                    
    java.lang.NullPointerException                 
        at FileReader.readLines(FileReader.java:25)                   
        at FileReaderTest.mockTest(FileReaderTest.java:43)  
Mox
  • 2,355
  • 2
  • 26
  • 42
  • What stands in line 43 in FileReaderTest? Where are you calling the `readLines` in your test? – alea Jul 12 '20 at 08:10
  • @alea `when(fileReader.readLines(any(Path.class))).thenReturn(lines);` this is the line that is throwing error due to nullpointer exception on the parameter that was passed into the method. – Mox Jul 12 '20 at 08:12
  • if the method is mocked, then it shouldn't be calling the actual method and giving nullpointerexception – Mox Jul 12 '20 at 08:13
  • 2
    Does this answer your question? [Final method mocking](https://stackoverflow.com/questions/3793791/final-method-mocking) – alea Jul 12 '20 at 08:27
  • 1
    @alea yap thanks. I didn't know `final` key word here refers to the method. not the return type. – Mox Jul 12 '20 at 08:30

0 Answers0