Java Spring测试Junit引发空指针异常
我有以下代码用于测试:Java Spring测试Junit引发空指针异常,java,testing,junit,spring-test,Java,Testing,Junit,Spring Test,我有以下代码用于测试: private static final Integer documentSetId = 1143; private static final Integer dsLifeCycleStateId = 1; private static final String dsLifecycleState = "CVS_CREATED"; CVBusiness cvBusinessTest; DocumentService documentService; Document
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)
它位于CVBusinessImpl扩展的CVBusiness包中
我的问题是为什么CVBusinessImpl中的documentService抛出空异常?
谢谢 也许您在测试中没有使用Spring?如何注入
documentService
将
@RunWith(SpringJUnit4ClassRunner.class)
添加到测试类中,以在测试中启用Spring。如果您使用autowire,则应仅此而已。您可能还需要在测试中添加@ContextConfiguration
注释和/或向要注入的成员添加@Resource
。在设置中,您创建了要测试的类:
cvBusinessTest = new CVBusinessImpl();
它需要的服务之一是:
documentService = mock(DocumentService.class);
但你从不把它们连在一起。因此,当您的CVBusinessImpl
实现调用时:
DocumentSet documentSet = documentService.getDocumentSetById(id)
documentService
仍为null
在测试之前,您应该通过使用Spring测试运行程序或设置该字段来连接对象。设置方法中的某些内容,如:
cvBusinessTest.setDocumentService(documentService);
你能发布异常吗?异常发生在哪里,哪一行?当我取消注释这一行时会发生异常:retValue=cvBusinessTest.markedForTranslation(documentSetId);并参考了CVBusiness.java第100行,其中包含我在上面发布的这个方法(DocumentSet DocumentSet=documentService.getDocumentSetById(id))是的,我有include@RunWith(SpringJUnit4ClassRunner.class),但我认为问题在于documentService注入。我不包括任何上下文配置xml。我是否必须在xml上下文中创建documentService的模拟实例?因为我认为我已经在我的代码中这样做了,因为它在生产中是如何进行注入的?自动连线,属性设置器?基于XML还是基于注释?在测试中使用类似的设置(一个小xml文件,测试中的一些@Resource注释,…)