java - Mocking a interface with Mockito -
can please me below mock object. want write mock test case class. want mock interface
public interface orderif{ list<order> ordersfor(string type); }
and implementation doesn't exists.
public class serviceimpl implements service { private list <order> orders ; private orderif orderif ; // 3rd party interface public int getval(string type) { //some code // returns list of objects (orders) orders = orderif.ordersfor(type); // code return orders.get(0) }
i trying below no luck. receving nullpoinerexcetion.
public class serviceimpltest { private list <order> ll ; private service reqservice ; @injectmocks private orderif order; @before public void setup() throws exception { ll = new arraylist<order> (); ll.add(new order("buy" , 11 , "usd" )); ll.add(new order("sell" , 22 , "usd" )); reqservice = spy(new serviceimpl()); } @test public void test() { string type= "usd" ; when(order.ordersfor(type)).thenreturn(ll); q = reqservice.getval(type); asserttrue(q.get().ask == 232.75); } }
i'm guessing order
null
, you're getting nullpointerexception
here:
when(order.ordersfor(type)).thenreturn(ll);
for @injectmocks
work , instantiate class, you'll need add runner:
@runwith(mockitojunitrunner.class) public class serviceimpltest { // ... }
you don't have use runner, refer the documentation alternatives.
Comments
Post a Comment