java - With Mockito, if I inject an object into my mock with @InjectMocks how do I avoid nulling out other objects? -
public class targetobjectwrapper extends targetobject { @mock someobject mymockofsomeobject; @injectmocks targetobject targetobject; public targetobjectwrapper(){ this.targetobject = mock(originalobject); //doanswer(...).when(...) methods } //override other methods of targetobject }
i'm trying inject specific mock object 1 field of targetobject
. know targetobject have field specific signature.
this works, can inject mymockofsomeobject
successfully, , redirect calls appropriately.
but when targetobject class contains other fields simple illustration below, field gets nulled out , causes null pointer exceptions when pass requests on original object.
log log = logfactory.getlog(getclass());
how can avoid causing other objects null values? if other variables might alter object object (this code should generic)?
it seems spy(..)
rather mock(...)
right way go in case. though haven't yet tested injected mock object, believe did.
java unit-testing mockito
No comments:
Post a Comment