首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spock中填充

Spock中填充
EN

Stack Overflow用户
提问于 2015-11-06 13:42:51
回答 2查看 2.2K关注 0票数 0

我如何存根/模拟一个填充了一些稍后要用到的对象的void方法。

代码语言:javascript
复制
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类的方法。

EN

回答 2

Stack Overflow用户

发布于 2015-11-08 22:15:44

在这种情况下,如果不对rh文件进行验证,您只需要一个普通的Stub -只是为了确保在测试docall方法时不会抛出NullPointerExceptionMock也足够了,但是它是更高级的对象,在这里使用它是没有意义的。当涉及到Spy时,它用于验证对真实对象的调用(就不被模拟而言)。看看下面的例子--只需使用Stub就可以顺利运行

代码语言:javascript
复制
@Grab('org.spockframework:spock-core:1.0-groovy-2.4')
@Grab('cglib:cglib-nodep:3.1')

import spock.lang.*

class Test extends Specification {
    def 'spec'() {
        given:
        def service = new ServiceClient()    
        service.rh = Mock(RequestHelper)

        when:
        service.doCall(new Order())

        then:
        noExceptionThrown()
    }
}

class Order {
    String product
    String quantity
    String profile
}

class OrderType { }

class FACTORY {
    static OrderType CreateOrderType = new OrderType()
}

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);
   }

    public setRequestHelper(RequestHelper rh){
        this.rh=rh;
    }
    public RequestHelper getRequestHelper(){
        return this.rh;
    }
}
票数 0
EN

Stack Overflow用户

发布于 2015-12-02 11:40:24

与Opal的答案非常相似,但使用的是模拟订单。

代码语言:javascript
复制
class Test extends Specification {
def 'spec'() {
    given:
    def service = new ServiceClient()
    def order = Mock(Order)
    order.getProduct() >> 'product1'
    order.getProfile() >> 'profile1'
    order.getQuantity() >> 3


    service.rh = Mock(RequestHelper)

    when:
    service.doCall(order)

    then:
    noExceptionThrown()
    1 * rh.populateOrderRequestBody('product1',3,'profile1',FACTORY.CreateOrderType)
}
}

请注意,只有当CreateOrderType.equals()返回true时,这才有效

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33560240

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档