有人可以請幫我下面的Mock對象。 想寫這個類的模擬測試用例。想模擬接口嘲笑與Mockito的接口
public interface orderIf{
List<Order> ordersFor(String type);
}
並且這個實現不存在。
public class serviceImpl implements service {
private List <Order> orders ;
private orderIF orderif ; // this is 3rd party interface
public int getval(String type) {
//some code
// this returns a list of objects (orders)
orders = orderif.ordersFor(type);
// some code
return orders.get(0)
}
我想下面沒有運氣。接受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);
}
}
而你的代碼在哪裏提出NPE? – Ralf