我需要模拟一个类似于这样的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编写junit,但是因为Groovy元编程技术不适用于Java类,所以无法实现。通过我的研究,我发现JMockit是一个很好的框架,可以模拟使用new构造函数创建的对象。
是否有人能够向我展示如何在Java或Groovy中编写上述类的单元测试。
提前致谢。
这是我迄今为止使用JMockit尝试过的测试案例,但没有成功。
void testSend(){
def serviceUrl = properties.getProperty("PROP").toString()
new Expectations(){
{
HttpClient httpClient=new HttpClient();
httpClient.executeMethod(); returns null;
}
};
def responseXml = Helper.sendMessage(requestXml.toString(), serviceUrl)
}