0

The function template is

private static void processTextFile(String iFile, String tmpDirLoc, String tmpDrive, String iMode)

I want to test for NullPointerException in case when iFile is null. Since the original method processTextFile is Private in nature, so I am using reflection in my test case to access the method but I can't write the test case for AssertThrows condition. please help me writing this.

this is my code

    @Test()
    public void TC1_processTextFileTest() {
    Method method = crashParser.class.getDeclaredMethod("left",String.class,int.class);
    method.setAccessible(true);
    crashParser cp = new crashParser();
    String ifile=null;
    //cp.processTextFile(ifile,null,null,null);
    //NullPointerException ex= assertThrows(NullPointerException.class,()-> 
   //cp.processTextFile(ifile,null,null,null));
    //assertEquals("Array can't be null",ex.getMessage());
   }
  • 2
    It always left me confused why people think they should test private methods... – Smutje Mar 31 '20 at 13:19
  • Really what you are asking is how to call an inaccessible private method using reflection; see https://stackoverflow.com/questions/880365/any-way-to-invoke-a-private-method – Stephen C Mar 31 '20 at 13:54

1 Answers1

2

Instead of writing a Unit test for a private method, instead reference the public object or method that utilizes that private method to assert that some internal state is as intended.

Jason
  • 5,154
  • 2
  • 12
  • 22