'How to assign instance variable values in JUnit

I have a class that I want to test that looks like this

public class TestClass{

private File incoming;

private void checkFiles(){
  File[] listOfFiles = incoming.listFiles();
  CustomDetailClass customClass = someMethodToRetrieveCustomClass();

  for (File file: listOfFiles){
  // check the list of files using the customClass
 }
}

}

Now I want to test this with a test class that ends up looking like this

@Test
void testCheckFiles(){
   TestClass testClass = new TestClass();

   assertDoesNotThrow(() -> testClass.checkFiles());
}

And I know it doesn't work and it gives me an error because incoming.listFiles() will return null and therefore listOfFiles will be null and the foreach loop won't work.

Is there a way where I can set the listOfFiles value in JUnit to a preset value? Something like incoming.listFiles() >> myOwnFileListICreated?

And is there a way to call a private method and set the return value explicitly as well? someMethodToRetrieveCustomClass() >> myOwnCustomDetailClass?

Many thanks for your help



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source