【问题标题】:Mocking RestOperations.exchange in Spock (overloaded method with varargs)在 Spock 中模拟 RestOperations.exchange(带有可变参数的重载方法)
【发布时间】:2020-04-10 07:16:50
【问题描述】:

我正在尝试在 Spock 中模拟 org.springframework.web.client.RestOperations.exchange。 Spock 失败了

Too few invocations for:

1 * restOperations.exchange("https://test.com", HttpMethod.POST, _ as HttpEntity, String)   (0 invocations)

Unmatched invocations (ordered by similarity):

1 * restOperations.exchange('https://test.com', POST, <whatever,[]>, class java.lang.String, [])

我认为问题与exchange 方法重载和我尝试调用的版本具有可变参数有关。

如何定义这种交互,以便测试成功?

MySubject.java:


import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.web.client.RestOperations;

public class MySubject {
    private final RestOperations rest;

    public MySubject(RestOperations rest) {
        this.rest = rest;
    }

    public void doStuff() {
        HttpEntity<String> httpEntity = new HttpEntity<>("whatever");
        rest.exchange("https://test.com", HttpMethod.POST, httpEntity);
    }
}

MyTest.groovy:


import org.apache.http.HttpEntity
import org.springframework.http.HttpMethod
import org.springframework.web.client.RestOperations
import spock.lang.Specification

class MyTest extends Specification {
    RestOperations restOperations = Mock(RestOperations)
    MySubject subject = new MySubject(restOperations)

    def "test"() {
        when:
        subject.doStuff()
        then:
        1 * restOperations.exchange("https://test.com", HttpMethod.POST, _ as HttpEntity, String)
    }
}

【问题讨论】:

    标签: spring-mvc mocking spock


    【解决方案1】:

    你有多个问题:

    1. 在您导入的应用程序中org.springframework.http.HttpEntity,在测试中org.apache.http.HttpEntity。你需要纠正它。

    2. 您的应用程序中的调用 rest.exchange("https://test.com", HttpMethod.POST, httpEntity); 甚至无法编译,因为在 RestOperations 类中没有这样的签名。需要添加参数String.class

    3. 在测试中您需要反映方法签名,包括可变参数,即真正的方法签名有 5 个参数。

    如果你解决了所有这些问题,你的测试就会顺利运行:

    package de.scrum_master.stackoverflow.q61135628;
    
    import org.springframework.http.HttpEntity;
    import org.springframework.http.HttpMethod;
    import org.springframework.web.client.RestOperations;
    
    public class MySubject {
      private final RestOperations rest;
    
      public MySubject(RestOperations rest) {
        this.rest = rest;
      }
    
      public void doStuff() {
        HttpEntity<String> httpEntity = new HttpEntity<>("whatever");
        rest.exchange("https://test.com", HttpMethod.POST, httpEntity, String.class);
      }
    }
    
    package de.scrum_master.stackoverflow.q61135628
    
    import org.springframework.http.HttpMethod
    import org.springframework.web.client.RestOperations
    import spock.lang.Specification
    
    class MyTest extends Specification {
      RestOperations restOperations = Mock()
      MySubject subject = new MySubject(restOperations)
    
      def "test"() {
        when:
        subject.doStuff()
    
        then:
        1 * restOperations.exchange("https://test.com", HttpMethod.POST, _, String, _)
        // Or if you want to be more specific:
    //  1 * restOperations.exchange("https://test.com", HttpMethod.POST, _, String, [])
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-26
      • 2016-03-06
      • 2012-05-02
      • 1970-01-01
      • 2017-06-29
      • 2021-12-23
      相关资源
      最近更新 更多