Spring mvc 在Spock中模拟RestOperations.exchange(使用varargs重载方法)
我试图在Spock中模拟Spring mvc 在Spock中模拟RestOperations.exchange(使用varargs重载方法),spring-mvc,mocking,spock,Spring Mvc,Mocking,Spock,我试图在Spock中模拟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 simi
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, [])
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)
}
}
您有多个问题:
org.apache.http.HttpEntity
中导入org.springframework.http.HttpEntity
。你需要纠正这一点rest.exchange(“https://test.com“,HttpMethod.POST,httpEntity)应用程序中的code>甚至不会编译,因为RestOperations
类中没有这样的签名。您需要添加参数String.class
package de.scrum_master.stackoverflow.q61135628;
导入org.springframework.http.HttpEntity;
导入org.springframework.http.HttpMethod;
导入org.springframework.web.client.rest操作;
公共类MySubject{
私人最终休息;
公共MySubject(rest操作rest){
这个。休息=休息;
}
公共空间{
HttpEntity HttpEntity=新的HttpEntity(“任何”);
其余.交换(”https://test.com,HttpMethod.POST,httpEntity,String.class);
}
}
package de.scrum\u master.stackoverflow.q61135628
导入org.springframework.http.HttpMethod
导入org.springframework.web.client.rest操作
导入spock.lang.Specification
类MyTest扩展了规范{
RestOperations RestOperations=Mock()
MySubject subject=新的MySubject(restOperations)
def“测试”(){
什么时候:
subject.doStuff()
然后:
1*restOperations.exchange(“https://test.com,HttpMethod.POST,\ u0,字符串,\ u0)
//或者,如果您想更具体一些:
//1*restOperations.exchange(“https://test.com“,HttpMethod.POST,200;,字符串,[])
}
}
您有多个问题:
org.apache.http.HttpEntity
中导入org.springframework.http.HttpEntity
。你需要纠正这一点rest.exchange(“https://test.com“,HttpMethod.POST,httpEntity)应用程序中的code>甚至不会编译,因为RestOperations
类中没有这样的签名。您需要添加参数String.class
package de.scrum_master.stackoverflow.q61135628;
导入org.springframework.http.HttpEntity;
导入org.springframework.http.HttpMethod;
导入org.springframework.web.client.rest操作;
公共类MySubject{
私人最终休息;
公共MySubject(rest操作rest){
这个。休息=休息;
}
公共空间{
HttpEntity HttpEntity=新的HttpEntity(“任何”);
其余.交换(”https://test.com,HttpMethod.POST,httpEntity,String.class);
}
}
package de.scrum\u master.stackoverflow.q61135628
导入org.springframework.http.HttpMethod
导入org.springframework.web.client.rest操作
导入spock.lang.Specification
类MyTest扩展了规范{
RestOperations RestOperations=Mock()
MySubject subject=新的MySubject(restOperations)
def“测试”(){
什么时候:
subject.doStuff()
然后:
1*restOperations.exchange(“https://test.com,HttpMethod.POST,\ u0,字符串,\ u0)
//或者,如果您想更具体一些:
//1*restOperations.exchange(“https://test.com“,HttpMethod.POST,200;,字符串,[])
}
}
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)
}
}