我有測試此代碼:春季測試的JUnit拋出空指針異常
private static final Integer documentSetId = 1143;
private static final Integer dsLifeCycleStateId = 1;
private static final String dsLifecycleState = "CVS_CREATED";
CVBusiness cvBusinessTest;
DocumentService documentService;
DocumentSetRepository documentSetRepository;
private DocumentSet documentSet;
private DSLifeCycleState dsLifeCycleState;
@Before
public void setUp(){
cvBusinessTest = new CVBusinessImpl();
documentService = mock(DocumentService.class);
documentSetRepository = mock(DocumentSetRepository.class);
documentSet = new DocumentSet();
dsLifeCycleState = new DSLifeCycleState();
documentSet.setActive(true);
documentSet.setDocumentSetId(documentSetId);
documentSet.setMarkedForTranslation(false);
dsLifeCycleState.setDsLifeCycleStateId(dsLifeCycleStateId);
dsLifeCycleState.setLabel(dsLifecycleState);
documentSet.setDsLifeCycleState(dsLifeCycleState);
when(documentService.getDocumentSetById(documentSetId)).thenReturn(documentSet);
when(documentService.updateDocumentSet(documentSet)).thenReturn(documentSet);
when(documentSetRepository.findOne(documentSetId)).thenReturn(documentSet);
}
@Test
public void markedForTranslationTest() {
boolean retValue = true;
DocumentSet docSet = documentService.getDocumentSetById(documentSetId);
dsLifeCycleState = docSet.getDsLifeCycleState();
if (dsLifeCycleState.getLabel().equals(LifeCycleStateEnum.CVS_CREATED.message()) && docSet.isActive() && !docSet.isMarkedForTranslation() && ((docSet.getfDR() == null) || docSet.getfDR().equals(""))){
documentSet.setMarkedForTranslation(true);
retValue = documentService.updateDocumentSet(documentSet) != null;
}
// retValue = cvBusinessTest.markedForTranslation(documentSetId);
assertTrue(retValue);
}
,當我運行JUnit,它有錯誤的完成: java.lang中的空指針異常。
哪些錯誤指向波紋管
DocumentSet documentSet = documentService.getDocumentSetById方法(ID)
這在CVBusiness包由CVBusinessImpl延長。
我的問題是爲什麼documentService在CVBusinessImpl引發空異常? 謝謝!
您可以發佈例外嗎? – Augusto
發生異常的地方是哪一行? –
當我取消註釋此行時發生異常: retValue = cvBusinessTest.markedForTranslation(documentSetId); 並引用CVBusiness.java行100包含此方法我有上面的帖子(DocumentSet documentSet = documentService.getDocumentSetById(id)) – wolver