2014-02-14 64 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);