Abhishek Kumar
Abhishek Kumar

Reputation: 39

Issue in writing junit test case for NullPointerException for private methods

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());
   }

Upvotes: 0

Views: 606

Answers (1)

Jason
Jason

Reputation: 5246

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.

Upvotes: 2

Related Questions