2014-02-14 69 views
-1

我有測試此代碼:春季測試的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引發空異常? 謝謝!

+1

您可以發佈例外嗎? – Augusto

+0

發生異常的地方是哪一行? –

+0

當我取消註釋此行時發生異常: retValue = cvBusinessTest.markedForTranslation(documentSetId); 並引用CVBusiness.java行100包含此方法我有上面的帖子(DocumentSet documentSet = documentService.getDocumentSetById(id)) – wolver

回答

0

也許你錯過了在你的測試中使用Spring?如何注入documentService

@RunWith(SpringJUnit4ClassRunner.class)添加到您的測試類,以在測試中啓用Spring。如果你使用autowire應該是全部。您可能還需要對您的測試註釋@ContextConfiguration和/或將@Resource添加到要注入的成員。

+0

是的,我有include @RunWith(SpringJUnit4ClassRunner.class),但我認爲問題是documentService注入。我不包含任何上下文配置xml。我應該創建一個documentService的模擬實例到上下文conf xml中嗎?因爲我認爲我已經在我的代碼中完成了,因爲它高於 – wolver

+0

您的注射是如何在生產中發生的? Autowired,屬性設定器?基於XML還是註釋?在你的測試中使用類似的設置(一個小的XML文件,在你的測試中的一些@Resource註釋,...) –

0

在你的設置,您可以創建你的類測試:

cvBusinessTest = new CVBusinessImpl(); 

,它需要的服務之一:

documentService = mock(DocumentService.class); 

但你永遠不將它們連接在一起。所以,當你的CVBusinessImpl實現調用:

DocumentSet documentSet = documentService.getDocumentSetById(id) 

documentService仍然null

您應該在測試之前連線對象,或者通過使用Spring測試運行器或通過設置該字段。在您的設置方法中的某些內容如下:

cvBusinessTest.setDocumentService(documentService);