lundi 28 mars 2016

NullPointerException while initializing an Object reference - Junit test method

I have a method 'validateFile()' which needs to be unit tested.The method returns a reference of a user defined type. This reference is set with a value returned from another method 'M'.This method 'M' is mocked and returns the mocked result.However setting the reference throws a NullPointerException. The code is as shown :

@Test
public void sampleTest(){
ExcelSheetIngestion excelSheetIngestion = new ExcelSheetIngestion();
PowerMockito.mockStatic(MiscellaneousService.class);
Mockito.when(MiscellaneousService.getRandomString()).thenReturn("mockedString123");

FileResponse response = excelSheetIngestion.validateFile(streamTest);
Assert.assertNotEquals("null",response.getBatchId());

}

//The method to be tested
public FileResponse validateFile(){
FileResponse response = new FileResponse();
String randomString = MiscellaneousService.getRandomString();//mocked and return mockedString123
response.setBatchId(randomString);//throws exception at this point
response.setError(errorList); 
return response;
}

Any clue as to where I have gone wrong would be helpful.

Aucun commentaire:

Enregistrer un commentaire