2012-08-28 5 views
0

java로 작성된 클래스에 대한 테스트 케이스를 groovy에 작성하려고합니다. Java 클래스 (이름 : Helper)에는 HttpClient 객체가 얻어지고 executeMethod가 호출되는 메소드가 있습니다. 나는이 httpClient.executeMethod()를 groovy 테스트 케이스에서 조롱하려고하는데, 제대로 조롱 할 수 없다. 다음은 Groovy에서 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); 

} 
} 

내가 지금까지 그루비에서 작성한 테스트 케이스는 다음과 같습니다 이유에

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) 

      } 
     } 

어떤 아이디어 그것은 정확하게 조롱하지 않습니다. 사전 감사

+0

[이 접근법] (http://thecarlhall.wordpress.com/2010/03/25/unit-testing-with-httpclients-localtestserver/)이 도움이 될 수 있습니다. –

답변

0

글쎄! 정적 메서드를 테스트하는 것이 매우 어렵고 로컬 변수를 속성으로 선언하지 않는 한 로컬 변수를 테스트하는 것이 더 어렵습니다. 정적 클래스에 대한 내 결론은 때때로 디자인에 관한 것입니다. 왜냐하면 여러분은 그 코드 블록을 다른 장소에 놓고 재사용 할 수 있기 때문입니다. 어쨌든, 여기 내 접근 방식은 모의 및 MOP이 경우에, 스텁, 아무것도 테스트하는 것입니다 :

이 클래스 같은 자바 클래스 :

import java.text.SimpleDateFormat; 
import java.text.ParseException; 
import java.util.Date; 

public class Helper{ 

    public static String message(final String serviceUrl) throws ParseException{ 
    SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yy"); 
    Date d = formatter.parse(serviceUrl); 
    String code = d.toString(); 
    return code; 
    } 
} 

그리고 이것은 내 GroovyTestCase입니다 :

import groovy.mock.interceptor.MockFor 
import groovy.mock.interceptor.StubFor 
import java.text.SimpleDateFormat 

class TestHelper extends GroovyTestCase{ 

    void testSendMessageNoMock(){ 
    def h = new Helper().message("01-01-12") 
    assertNotNull h 
    println h 
    } 

    void testSendMessageWithStub(){ 
    def mock = new StubFor(SimpleDateFormat) 
    mock.demand.parse(1..1) { String s -> 
     (new Date() + 1) 
    } 
    mock.use { 
     def h = new Helper().message("01-01-12") 
     assertNotNull h 
    } 
    } 

    void testSendMessageWithMock(){ 
    def mock = new MockFor(SimpleDateFormat) 
    mock.demand.parse(1..1) { String s -> 
     (new Date() + 1) 
    } 
    shouldFail(){ 
     mock.use { 
     def h = new Helper().message("01-01-12") 
     println h 
     } 
    } 
    } 

    void testSendMessageWithMOP(){ 
    SimpleDateFormat.metaClass.parse = { String s -> 
     println "MOP" 
     new Date() + 1 
    } 
    def formatter = new SimpleDateFormat() 
    println formatter.parse("hello world!") 
    println " Exe: " + new Helper().message("01-01-12") 
    } 
} 

질문에 대한 답은 아마도 방법의 로컬 변수이며 테스트 할 공동 작업자가 아니기 때문일 수 있습니다.

감사

0

그것은 조롱 객체가 인스턴스화되지 않도록 HttpClient를 인스턴스를 생성 할 때 컴파일 된 자바 클래스는 Groovy의 메타 객체 프로토콜 (MOP)를 통해 이동하지 않기 때문에 작동하지 않습니다.

HttpClient 인스턴스가 스레드로부터 안전하기 때문에 종속성으로 클래스에 주입하는 방법에 대해 생각해 볼 수 있습니다. 그러면 테스트에서 단순히 mock을 주입 할 수 있습니다.

+0

좀 더 구체적으로 말씀해 주시겠습니까? 또는 그것을 달성하는 방법에 대한 예제가 있으십니까 – Npa

+0

더 많은 옵션 ... – Npa

+0

가능하면 해결 방법이나 관련 리소스에 매우 관심이 있습니다. 고마워. –