gpt4 book ai didi

java - spock 测试没有正确设置返回值

转载 作者:行者123 更新时间:2023-11-29 05:20:56 24 4
gpt4 key购买 nike

所以我正在尝试测试以下方法在 statusCode 为 401 时抛出异常:

HttpEntity doGet(HttpGet httpGet) {
# other stuff...
HttpResponse response = this.httpClient.execute(httpGet); // LINE 3
int statusCode = response.getStatusLine().getStatusCode(); // LINE 4
if (statusCode == 401) {
throw new ApiClientException(401, "Recheck your login username & password credentials in the " +
"file Configurations.groovy as they are NOT working.");
}
# other stuff...
}

我正在使用 Spock 测试框架,您可以在其中使用“>>”指定对象方法的返回值。因此,当代码 response.getStatusLine().getStatusCode() 被调用时,我想控制它在上面的第 4 行返回 401。

我试图在以下测试的第 18 和 19 行执行此操作,但它不起作用:

def "test doGet(HttpGet httpGet) throws the correct exceptions when given unsuccessful HttpGet instance"() {
given:
def httpGet = new HttpGet("http://sand.api.appnexus.com/member?id=1196");

def httpClient = Stub(HttpClient);
this.apiClient.httpClient = httpClient;

def httpResponseWithStatusCode401 = Stub(HttpResponse);

httpClient.execute(httpGet) >> httpResponseWithStatusCode401; # LINE 18 This response is correctly returning

httpResponseWithStatusCode401.getStatusLine().getStatusCode() >> 401; # LINE 19 this is never returning 401 right now and always 0 instead

when:
ApiClientException expectedException;
try {
this.apiClient.doGet(httpGet);
} catch(ApiClientException expected) {
expectedException = expected;
}

then:
expectedException.getMessage().equals("Recheck your login username & password credentials in the " +
"file Configurations.groovy as they are NOT working.");
}

问题:如何让第 4 行在第 19 行的测试中返回我想要的内容?

最佳答案

这就是你应该如何实现这个测试:

@Grab('org.spockframework:spock-core:0.7-groovy-2.0')
@Grab('cglib:cglib-nodep:3.1')
@Grab('org.apache.httpcomponents:httpclient:4.3.4')

import spock.lang.*
import org.apache.http.client.methods.HttpGet
import org.apache.http.client.HttpClient
import org.apache.http.HttpEntity
import org.apache.http.HttpResponse
import org.apache.http.StatusLine
import org.apache.http.message.BasicHttpResponse

class Test extends Specification {

def "test doGet(HttpGet httpGet) throws the correct exceptions when given unsuccessful HttpGet instance"() {
given:
def client = new Client()
client.httpClient = GroovyMock(HttpClient) {
execute(_) >> new BasicHttpResponse(null, 401, '')
}

when:
client.doGet(GroovyMock(HttpGet))

then:
def e = thrown(ApiClientException)
e.code == 401
e.message == 'Recheck your login username & password credentials in the file Configurations.groovy as they are NOT working.'
}
}

class ApiClientException extends Exception {

def code
def msg

ApiClientException(code, msg) {
this.code = code
this.msg = msg
}

String getMessage() {
'Recheck your login username & password credentials in the file Configurations.groovy as they are NOT working.'
}
}

class Client {

def HttpClient httpClient

HttpEntity doGet(HttpGet httpGet) {

HttpResponse response = httpClient.execute(httpGet)
int statusCode = response.getStatusLine().getStatusCode()
if (statusCode == 401) {
throw new ApiClientException(401, "Recheck your login username & password credentials in the " +
"file Configurations.groovy as they are NOT working.");
}
}
}

你都清楚了吗?

关于java - spock 测试没有正确设置返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24789362/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com