【问题标题】:Stub void method in Spock which populateSpock中的Stub void方法填充
【发布时间】: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


【解决方案1】:

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

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

【讨论】:

  • 对不起,我不明白:你说用Stub,但你好像用过Mock……这有什么原因吗?
【解决方案2】:

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

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 时有效

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-05
    • 2017-07-19
    • 2021-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多