I have this code for testing :

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

}

and when i run Junit, it finished with errors: java.lang null pointer exception.

Which error points the method bellow

DocumentSet documentSet = documentService.getDocumentSetById(id)

which is in CVBusiness package extended by CVBusinessImpl.

My question is why documentService in CVBusinessImpl throws Null exception? Thanks!

有帮助吗?

解决方案

Maybe you missed to use Spring in your Test? How the documentService is injected?

Add @RunWith(SpringJUnit4ClassRunner.class) to your test class to enable Spring in the test. If you use autowire that should be all. You may also need a @ContextConfiguration annotation to your test and/or add @Resource to the members to be injected.

其他提示

In your setup, you create your class to test:

cvBusinessTest = new CVBusinessImpl();

and one of the services it requires:

documentService = mock(DocumentService.class);  

but you never wire them together. So when your CVBusinessImpl implementation calls:

DocumentSet documentSet = documentService.getDocumentSetById(id)

documentService is still null.

You should wire your objects before testing, either through using a Spring test runner or through setting that field. Something in your setup method like:

cvBusinessTest.setDocumentService(documentService);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top