我正在尝试用 groovy 为用 java 编写的类编写一个测试用例。Java 类(名称:Helper)中有一个方法,其中获取 HttpClient 对象并在其上调用 executeMethod。我试图在常规测试用例中模拟这个 httpClient.executeMethod() ,但无法正确模拟它。
下面是Java类 //这个帮助类是一个java类
public class Helper{
public static message(final String serviceUrl){
----------some code--------
HttpClient httpclient = new HttpClient();
HttpMethod httpmethod = new HttpMethod();
// the below is the line that iam trying to mock
String code = httpClient.executeMethod(method);
}
}
到目前为止,我用 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)
}
}
关于为什么它没有正确模拟的任何想法。提前谢谢