Grails模拟使用rest插件的服务方法

时间:2015-02-27 16:53:31

标签: unit-testing grails grails-plugin

我是Grails的新手,但过去几个月一直在使用Ruby on Rails,我似乎无法理解如何正确地模拟服务中的某些功能,所以我可以正确地进行单元测试。

我有RestService使用RestBuilder插件

import javax.persistence.Converter;
import org.codehaus.groovy.grails.web.json.JSONArray
import org.json.JSONObject

import grails.converters.JSON
import grails.plugins.rest.client.RestBuilder
import grails.transaction.Transactional

@Transactional
class RestService {

    def retrieveFromRESTClient(url) {
        System.properties.putAll( ["http.proxyHost":"some.proxy.host.com", "http.proxyPort":"8080", "https.proxyHost":"some.proxy.host.com", "https.proxyPort":"8080"] )

        def restBuilder = new RestBuilder()
        def clientResponse = restBuilder.get(url)

        // For development purposes 
        print "retrieveFromRESTClient: " + clientResponse.json

        return clientResponse
    }
}

我试图为retrieveFromRESTClient()编写一个单元测试,我的想法是我应该模拟restBuilder.get()插件调用,这样它就不会出现并实际获取在测试期间请求URL。我已经尝试过一些事情,例如将插件功能提取到它自己的方法中:

def retrieveFromRESTClient(url) {
    System.properties.putAll( ["http.proxyHost":"some.proxy.host.com", "http.proxyPort":"8080", "https.proxyHost":"some.proxy.host.com", "https.proxyPort":"8080"] )

    def clientResponse = getResponse(url)

    // For development purposes 
    print "retrieveFromRESTClient: " + clientResponse.json

    return clientResponse
}

def getResponse(url) {
    def restBuilder = new RestBuilder()
    def resp = restBuilder.get(url)
    resp
}

并在RestServiceSpec试图模仿getResponse

import org.springframework.http.ResponseEntity;

import grails.plugins.rest.client.RestBuilder;
import grails.test.mixin.TestFor
import groovy.mock.interceptor.MockFor
import spock.lang.Specification

@TestFor(RestService)
class RestServiceSpec extends Specification {

    def cleanup() {
    }

    void "retrieveFromRESTClient's responses JSON can be accessed"() {      
        when:
        service.metaClass.getResponse { ResponseEntity<String> foo -> return new ResponseEntity(OK) }
        def resp = service.retrieveFromRESTClient("http://mocking.so.it.doesnt.matter/")

        then:
        print resp.dump()
        assert resp.json == [:]
    }
}

虽然这个测试通过,但是当我在测试报告中查看resp.dump()时,我发现它仍然消失了,并向'mocking.so.it.doesnt.matter&#发出请求# 39;并且返回了该对象,而不是我认为会返回的模拟的ResponseEntity。

测试报告输出:

retrieveFromRESTClient: [:]<grails.plugins.rest.client.RestResponse@433b546f responseEntity=<404 Not Found,<HTML><HEAD>
<TITLE>Network Error</TITLE>
<style>
body { font-family: sans-serif, Arial, Helvetica, Courier; font-size: 13px; background: #eeeeff;color: #000044 }
li, td{font-family: Arial, Helvetica, sans-serif; font-size: 12px}
hr {color: #3333cc; width=600; text-align=center}
{color: #ffff00}
text {color: #226600}
a{color: #116600}
</style>
</HEAD>
<BODY>
<big><strong></strong></big><BR>
<blockquote>
<TABLE border=0 cellPadding=1 width="80%">
<TR><TD>
<big>Network Error (dns_unresolved_hostname)</big>
<BR>
<BR>
</TD></TR>
<TR><TD>
RYLCR-BC-20
</TD></TR>
<TR><TD>
Your requested host "mocking.so.it.doesnt.matter" could not be resolved by DNS.
</TD></TR>
<TR><TD>

</TD></TR>
<TR><TD>
<BR>

</TD></TR>
</TABLE>
</blockquote>
</BODY></HTML>
,{Cache-Control=[no-cache], Pragma=[no-cache], Content-Type=[text/html; charset=utf-8], Proxy-Connection=[close], Connection=[close], Content-Length=[784]}> encoding=UTF-8 $json=[:] $xml=null $text=null>

我的最终目标是绕过插件get调用并返回ResponseEntity对象。我不确定我是否使用了正确的方法吗?

1 个答案:

答案 0 :(得分:2)

在您的单元测试中,您模拟了一个期待

的方法
ResponseEntity<String> foo 

作为参数。

然后你打电话:

service.retrieveFromRESTClient("http://mocking.so.it.doesnt.matter/")

参数类型为String。因此,您的模拟方法未被调用(方法签名不同)。

将模拟方法更改为:

service.metaClass.getResponse { String foo -> return new ResponseEntity(OK) }

它应该有用。