anunaki
anunaki

Reputation: 2264

Stub void method in Spock which populate

How can I stub/mock a void method which populates some objects that would be used later.

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

Now I want to test ServiceClient class which call RequestHelper to populate orderType object. How to stub the method of RequestHelper class.

Upvotes: 0

Views: 2836

Answers (2)

David W
David W

Reputation: 945

Very similar to Opal's answer but using a mock order..

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

Note that this only works if the CreateOrderType.equals() will return true

Upvotes: 0

Opal
Opal

Reputation: 84844

In this particular case if no verification will be done to rh filed you just need a plain Stub - just to ensure no NullPointerException is thrown when testing the docall method. Mock will also be sufficient however is more advanced object and using it here is pointless. When it comes to Spy it's used to verify invocations on a real (in terms of not being mocked) object. Have a look at the example below - runs smoothly just with 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;
    }
}

Upvotes: 2

Related Questions