可能重复:
Unit Testing a static method in a Java Class from Groovy Test case惩戒的HttpClient在Groovy
我使用试图单元测试java类常规的Helper
具有其中获得一个HttpClient
在它的静态方法和executeMethod
被调用。为了对这个类进行单元测试,我试图在Groovy测试用例中嘲笑这个httpClient.executeMethod()
,但是我无法正确地嘲笑它。
下面是一个Java类
public class Helper{
public static message(final String serviceUrl){
HttpClient httpclient = new HttpClient();
HttpMethod httpmethod = new HttpMethod();
// the below is the line that iam trying to mock
String code = httpClient.executeMethod(method);
}
}
如何单元测试在Groovy这个静态方法什么想法?由于httpClient
对象位于类方法内部,因此如何在Groovy测试用例中嘲笑此对象?
这是我迄今为止的测试案例。我试图嘲弄为空,但不会发生......
void testSendMessage(){
def serviceUrl = properties.getProperty("ITEM").toString()
// mocking to return null
def mockJobServiceFactory = new MockFor(HttpClient)
mockJobServiceFactory.demand.executeMethod{ HttpMethod str ->
return null
}
mockJobServiceFactory.use {
def responseXml = helper.message(serviceUrl)
}
}
也许[this](http://thecarlhall.wordpress.com/2010/03/25/unit-testing-with-httpclients-localtestserver/)可以帮助你。 –