unit testing - How to inject mock in parent class with Mockito? -
i'm using mockito mock services..
how can inject mocks in parent class?
sample:
public abstract class parent(){ @mock message message; } public class mytest() extends parent{ @injectmocks myservice myservice //myservice has instance of message //when put @mock message here works }
when run tests message in parent
stay null
two ways solve this:
1) need use mockitoannotations.initmocks(this)
in @before method in parent class.
the following works me:
public abstract class parent { @mock message message; @before public void initmocks() { mockitoannotations.initmocks(this); } } public class mytest extends parent { @injectmocks myservice myservice = new myservice(); //myservice has instance of message ... }
2) if want use @runwith(mockitojunitrunner.class) above class definition, must done in parent class:
@runwith(mockitojunitrunner.class) public class parent { ...
note declaring @mock in both parent , mytest class cause injected object null. have pick want declare this.
Comments
Post a Comment