Mocking RestOperations.exchange в Spock (перегруженный метод с varargs) - PullRequest
0 голосов
/ 10 апреля 2020

Я пытаюсь высмеять org.springframework.web.client.RestOperations.exchange в Споке. Спок терпит неудачу с

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 перегружен, а версия, которую я пытаюсь вызвать, имеет аргументы vararg.

Как сделать Я определяю это взаимодействие, чтобы проверка прошла успешно?

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

1 Ответ

2 голосов
/ 10 апреля 2020

У вас есть несколько проблем:

  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, [])
  }
}
...