2012-09-04 131 views
2

我需要在一個Java類,它是這樣嘲笑的方法:懲戒使用JMockit

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的,但不能這樣做,因爲grrovy元proggraming技術不要申請java類。在我的研究中,我發現JMockit是一個很好的框架,它也可以模擬使用新構造函數創建的對象。

有人可以告訴我如何在java或Groovy中編寫上述類的unittest。

高級感謝

這是我迄今爲止嘗試使用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)    
} 
+0

有你用JMockit試過了什麼嗎?我知道你最後的,基本相同的問題建議JMockit,還有Mockito和EasyMock。例子比比皆是 - 你有什麼嘗試? –

+0

我在我的問題中添加了一個編輯,顯示我寫的測試....由於iam正在研究的模擬對象是httpClient,如果我使用java或用groovy編寫測試用例,您認爲它會更好嗎 – Npa

回答

2

測試用例的Java版本會是什麼樣子:

@Test 
public void testSend() throws IOException { 

    final String serviceUrl = "http://google.com/"; 

    new Expectations(){ 
     // these bits are important as they tell Jmockit what classes to mock 
     @Mocked HttpClient client ; 
     @Mocked GetMethod method; 

     { 
      HttpClient httpClient= new HttpClient() ; 
      HttpMethod method = new GetMethod(withEqual(serviceUrl)); 
      try { 
       httpClient.executeMethod(method); 
      } catch (IOException e) { 
       // not going to happen 
      } 
      result = 200; 
     } 
    }; 
    // run the test and assert something 
    Assert.assertEquals(200, Helper.message(serviceUrl)); 
} 

這是在github.com,注意我使用httpClient 3.1來實現您的消息方法,我猜這是不完全正確的,但應該足以回答題。

如果你可以準備一個簡單的Grails項目與你的測試用例,我相信我可以找出問題所在。

更新:我一直在玩本地玩具grails項目,並沒有設法正確配置jmockit。 jmockit的關鍵是確保它在classpath之前是junit,但由於junit在grails中發佈,我無法找到將jmockit放在正確的位置。

+0

謝謝回覆。我如何在我的Grails項目中開始使用Jmockit。我在Grails插件管理器中檢查了Jmockit插件,但沒有找到任何插件。 – Npa

+0

這就是爲什麼一個簡單的grails項目設置與您在項目中完成的相同方式將是最有用的。 JMockit的一個棘手問題是類路徑設置,具體而言,它需要在JUnit之前位於CP上。 –

1

用jmockit你也可以模擬實例創建。我喜歡稍微不同的技術:

@Test  
public void testFoo(@Mocked final HttpClient client) { 
     new Expectations() {{ 
       // expect instance creation and return mocked one 
       new HttpClient(...); returns(client) 

       // expect invocations on this mocked instance 
      client.invokeSomeMethid(); returns(something) 
     }}; 


     helper.message(serviceUrl) 
} 
+0

感謝您的回覆。我如何在我的Grails項目中開始使用Jmockit。我在Grails插件管理器中檢查了Jmockit插件,但沒有找到任何插件。 – Npa

+0

不知道Grails插件架構是如何工作的。但提供了jmockit之前,在測試線束它可以儀器註釋類之前的類路徑。 jmockit專用列表maz是更好的信息源 –

0

感謝提出的問題,與Spring的web-3.2.2(使用的HttpClient-4.0.1),我的代碼看起來是這樣的:

new NonStrictExpectations(){ 
     @Mocked(methods={"executeMethod"}) 
     ClientHttpRequest mocked_req; 
     @Mocked(methods={"getBody"}) 
     ClientHttpResponse mocked_res; 
     { 
      byte[] buf = (
      "<example_xml><person><name>Johnson</name><age>20</age></person></example_xml>" 
      ).getBytes(); 

      mocked_req.execute(); 
      mocked_res.getBody(); returns(new ByteArrayInputStream(buf)); 

     } 
    }; 


    RestTemplate mytemplate = new RestTemplate(); 
    obj =   mytemplate.getForObject(.....); 
    assertEquals("returned and parsed obj should be equal to this one", expectedObj, obj);