title: How to mock FileWriter in Junit 5
body: <p>I am trying to mock a FileWriter so that my test will not create the file. The code is running ordinary but I can not create a proper test.</p>
  
  <p>JSONExporter </p>
  
  <pre><code>    public void exportFile(List&lt;Employee&gt; employeeList) throws ExportFileException {
      try (Writer writer = new FileWriter("filepath")) {
          Gson gson = new GsonBuilder()
                  .setPrettyPrinting()  //prints the json file with json indenting
                  .create();
          gson.toJson(employeeList, writer);
      } catch (IOException ex) {
          throw new ExportFileException();
      }
  }
  </code></pre>
  
  <p>TestJSONExporter</p>
  
  <pre><code> @Injectable FileWriter mockedFileWriter;
  @Test
  void testExportFile_whenFilenameIsValid_willDoNothing(@Mocked Gson mockedGson) throws IOException {
      new Expectations() {{
          new FileWriter(anyString); result = mockedFileWriter;
          mockedGson.toJson(any, (FileWriter) any);
      }};
      Assertions.assertDoesNotThrow( () -&gt; jsonFileExporter.exportFile("filename", Role.SELLER, employeeList));
  }
  </code></pre>
  
  <p>Console</p>
  
  <pre><code>java.io.FileNotFoundException:  (Result too large)
  
  at java.io.FileOutputStream.open0(Native Method)
  at java.io.FileOutputStream.open(FileOutputStream.java:270)
  at java.io.FileOutputStream.&lt;init&gt;(FileOutputStream.java:213)
  at java.io.FileOutputStream.&lt;init&gt;(FileOutputStream.java:101)
  at java.io.FileWriter.&lt;init&gt;(FileWriter.java:63)
  at com.eurodyn.training.export.JSONFileExporterTest$1.&lt;init&gt;(JSONFileExporterTest.java:59)
  at com.eurodyn.training.export.JSONFileExporterTest.testExportFile_whenFilenameIsValid_willDoNothing(JSONFileExporterTest.java:58)
  </code></pre>
  
  <p>Does anyone know if there is a way to do it with either jmockit or mockito. PowerMock is not compatible with JUnit 5 i think.</p>