unit testing - Mockito - Null Pointer Exception -
i have stupidly simple class (right now) takes id , passes along dao.
@service public class adhocreportserviceimpl { @autowired private adhocreportserviceimpl adhocreportdao; public list<adhocreportresponsedto> getadhocreportresults(string reportid) { return adhocreportdao.getadhocreportresults(reportid); } }
and i'm attempting use mockito verify logic there (this app has no code coverage , i'm trying change bit)
@runwith(mockitojunitrunner.class) public class adhocreportservicetest { @injectmocks private adhocreportserviceimpl adhocreportserviceimpl = new adhocreportserviceimpl(); @mock private adhocreportdao adhocreportdao; @test public void getadhocreportresultstest() { list<adhocreportresponsedto> adhocreportresponsedtolist = new arraylist<adhocreportresponsedto>(); adhocreportresponsedto adhocreportresponsedto1 = new adhocreportresponsedto(); adhocreportresponsedto adhocreportresponsedto2 = new adhocreportresponsedto(); adhocreportresponsedtolist.add(adhocreportresponsedto1); adhocreportresponsedtolist.add(adhocreportresponsedto2); when(adhocreportdao.getadhocreportresults(anystring())).thenreturn(adhocreportresponsedtolist); adhocreportserviceimpl.getadhocreportresults("anystring()"); } }
mockito saying i'm getting getting null pointer exception @ adhocreportdao. when go debug sure enough dao null in class that's being tested i'm not sure have done wrong , mockito documentation doesn't seem helping me out. thoughts?
your @autowired
field of type adhockreportserviceimpl; if want injected using @injectmocks you'll need make adhocreportdao in test.
whether or not root cause, 1 of reasons @injectmocks
fragile , should prefer explicit setter or constructor testing.
Comments
Post a Comment