title: Test File Selection with JUnit/JMockit
body: <p>I am attempting to test a method that returns a File object using JUnit and JMockit. I am a beginner with both of these.</p>
  
  <p>The problem I am having is that I can't figure out how to properly/successfully mock the implementation method returning a file, since in reality, the user has to actually select a file for the method to return. The error I keep running into is:</p>
  
  <pre><code>java.lang.IllegalStateException: Missing invocation to mocked type at this point; please make sure such invocations appear only after the declaration of a suitable mock field or parameter
  </code></pre>
  
  <p>Any suggestions?</p>
  
  <p>Here is a recreation of my implementation:</p>
  
  <pre><code>public final class MyClass {
      public static File OpenFile(Stage stage, String title, String fileTypeText, ArrayList&lt;String&gt; fileType) throws Exception {
          File file = null;
          try {
              FileChooser fileChooser = new FileChooser();
              fileChooser.setTitle(title);
              FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionsFilter(fileTypeText + fileType, fileType);
              fileChooser.getExtensionsFilters().add(extFilter);
              file = fileChooser.showOpenDialog(stage);
              return file;
          }
          catc (Exception e) {
              if(fileType==null) {
                  ...
              }
              return file;
          }
      }
  }
  </code></pre>
  
  <p>Here is a recreation of my attempted JUnit test:</p>
  
  <pre><code>@Test
  public void TestOpenFile(@Mocked Stage stage) throws Exception {
  
      final ArrayList&lt;String&gt; extensions = new ArrayList&lt;String&gt;();
      extensions.add(".txt");
  
      final File file = null;
  
      new Expectations() {{
          MyClass.OpenFile(stage, anyString, anyString, extensions); returns(file);
      }};
  
      assertEquals(file, MyClass.OpenFile(stage, "some title", "some type", extensions));
  
  }
  </code></pre>