【发布时间】:2015-11-06 05:42:51
【问题描述】:
如何存根/模拟填充一些稍后使用的对象的 void 方法。
class RequestHelper{
public void populateOrderRequestBody(String product,String quantity,String profile, OrderType orderType){
orderType.setProduct(product);
orderType.setQuantity(Integer.parseInt(quantity));
orderType.setUser(profile.getUserId());
} }
class ServiceClient{
RequestHelper rh;
public void docall(Order order){
OrderType orderType = FACTORY.CreateOrderType;
rh.populateOrderRequestBody(order.getProduct(),order.getQuantity(),order.getProfile(),orderType);
/**
* some other code
**/
}
public setRequestHelper(RequestHelper rh){
this.rh=rh;
}
public RequestHelper getRequestHelper(){
return this.rh;
}}
现在我想测试调用 RequestHelper 来填充 orderType 对象的 ServiceClient 类。如何存根 RequestHelper 类的方法。
【问题讨论】:
-
我想为ServiceClient类写一个测试类。而且我无法决定如何将 RequestHelper 类作为 Mock、Stub 或 spy。
-
如果您觉得我的回答有用,请接受并点赞。
标签: java unit-testing spock