初始化Object引用时出现NullPointerException - Junit测试方法

时间:2016-03-28 18:29:00

标签: java unit-testing mockito junit4 powermock

我有一个方法' validateFile()'需要进行单元测试的方法。该方法返回用户定义类型的引用。该引用设置为从另一种方法返回的值' M' M' M' M' M' M'被模拟并返回模拟结果。但是设置引用会抛出NullPointerException。 代码如下所示:

@RunWith(PowerMockRunner.class)
@PrepareForTest({ExcelValidations.class, MiscellaneousService.class})    
class TestClass(){

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

}

class ExcelSheetIngestion(){

  @Inject 
  FileUploadResponse  response;
  //The method to be tested
  public FileResponse validateFile(){

  String randomString = MiscellaneousService.getRandomString();//mocked and return mockedString123
  response.setBatchId(randomString);//throws exception at this point
  response.setError(errorList); 
  return response;
  }
}

关于我哪里出错的任何线索都会有所帮助。

0 个答案:

没有答案