2012-09-04 133 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);