首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用JMockit模拟

使用JMockit模拟
EN

Stack Overflow用户
提问于 2012-09-05 05:08:32
回答 3查看 8.5K关注 0票数 3

我需要模拟java类中的方法,如下所示:

代码语言:javascript
复制
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,但我不能这样做,因为复杂的元编程技术不适用于java类。在我的研究中,我发现JMockit是一个很好的框架,它还可以模拟使用新构造函数创建的对象。

有人能告诉我如何用java或groovy为上面的类编写单元测试吗?

高级致谢

这是我到目前为止使用jmockit尝试的测试用例,但不起作用。

代码语言:javascript
复制
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)            
}
EN

回答 3

Stack Overflow用户

发布于 2012-09-05 18:29:31

测试用例的java版本将如下所示:

代码语言:javascript
复制
@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实现了你的message方法,我猜这不是很正确,但应该足以回答这个问题。

如果你能用你的测试用例准备一个简单的grails项目,我相信我能找出问题所在。

JmockitUpdateJmockit.jmockitup.jmockit.jmockitup.jmockit.jmock使用jmockit的关键是要确保它在类路径中位于junit之前,但是由于junit是在grails中提供的,所以我找不到合适的位置来获取jmockit。

票数 2
EN

Stack Overflow用户

发布于 2012-09-05 18:39:26

使用jmockit,您还可以模拟实例创建。我更喜欢稍微不同的技术:

代码语言:javascript
复制
@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)
}
票数 2
EN

Stack Overflow用户

发布于 2013-05-10 04:43:00

感谢您提出这个问题,使用Spring-web-3.2.2 (它使用httpclient-4.0.1),我的代码如下所示:

代码语言:javascript
复制
    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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12271389

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档