Mocking Stream or Reader in Java Junit

28 Views Asked by At

I have following two services to generate a CSV file from a given file. And trying to test each services. However could not fig out how to mock Stream/Reader.

@Servce
public class MyService {
  public void process() {
    List<String> fileNames =  someService.collectFileNames();
    fileNames.forEach(currFile -> {
      try {
        FileInputStream currFileStream = new FileInputStream(currFile);
        InputStreamReader currFileStreamReader = new InoutStreamReader(currFileStream, UTF_8);
        Reader currFileReader = new BufferedReader(currFileStreamReader);
      } catch (FileNotFoundException e) { ... }
    }
   }
 }

 @Service
 public class MyFileProcessor {
   public void createCVSFile(Reader fileReader) {
     CSVParser csvParser = new CSVParser(fileReader, ...);
     ...
   }
 }
 

I am getting FileNotFoundExcpetion when mocking stream and even passing any() on mock function.

Any suggestion?

0

There are 0 best solutions below