가능한 중복 :
Unit Testing a static method in a Java Class from Groovy Test case도발 HttpClient를 그루비에서
내가 사용하는 단위 테스트에 자바 클래스를 시도하고 그루비 Helper
이있는 HttpClient
을 얻을 수 거기에 정적 방법 executeMethod
이 호출됩니다. 이 클래스를 단위 테스트하려면이 httpClient.executeMethod()
을 Groovy 테스트 케이스에서 조롱하려고 노력하고 있지만 올바르게 조롱 할 수는 없습니다.
다음은 자바 클래스는 어떻게 단위 테스트를 그루비에서이 정적 메서드에
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);
}
}
어떤 아이디어인가? httpClient
객체가 클래스 메소드 안에 있기 때문에 Groovy 테스트 케이스에서이 객체를 어떻게 조롱합니까?
이것은 지금까지 테스트 케이스입니다. 당신이 조롱하기가 매우 어려울 것입니다 자바 클래스의 정적 메소드 내에서 명시 적으로 HttpClient를 생성자를 호출하고 어디
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/)가 도움이 될 수 있습니다. –