单元从Groovy测试用例中测试Java类中的静态方法 [英] Unit Testing a static method in a Java Class from Groovy Test case

查看:131
本文介绍了单元从Groovy测试用例中测试Java类中的静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个用java编写的类的groovy测试用例。 Java类(名称:Helper)有一个静态方法,在该方法中获得HttpClient对象并调用executeMethod。为了Unittest这个类,我试图在groovy测试用例中模拟这个httpClient.executeMethod(),但是不能模拟它。



以下是Java类

  public class Helper {

public static message(final String serviceUrl){

HttpClient httpclient = new HttpClient();
HttpMethod httpmethod = new HttpMethod();

//下面是iam试图模拟
的代码行String code = httpClient.executeMethod(method);


$ b $ / code $ / pre

任何有关如何进行单元测试的想法这个来自groovy的静态方法。由于httpClient对象是类方法内的对象,我如何在groovy测试用例中模拟这个对象?



这是我迄今为止的测试用例。嘲笑null,但不会发生......

  void testSendMessage(){
def serviceUrl = properties.getProperty (ITEM)。toString()

//嘲笑返回null
def mockJobServiceFactory = new MockFor(HttpClient)
mockJobServiceFactory.demand.executeMethod {HttpMethod str - >
返回null
}

mockJobServiceFactory.use {
def responseXml = helper.message(serviceUrl)

}
}


解决方案

您可以使用

  HttpClient.metaClass.executeMethod = {类型名称 - > doSomething} 

您需要用正确的 Type 即字符串,映射等。

  void testSendMessage(){
def serviceUrl = properties。 getProperty(ITEM)。toString()

//嘲笑返回null
HttpClient.metaClass.executeMethod = {HttpMethod str - > null}
def responseXml = helper.message(serviceUrl)

}


I am trying to write a test case in groovy for a class that is written in java. The Java class(name:Helper) has a static method in it where a HttpClient object is obtained and executeMethod is called on it. To Unittest this class,I am trying to mock this httpClient.executeMethod() in groovy test case, but not able to mock it right.

Below is the Java class

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);

    }
}

Any Ideas on how to unit test this static method from groovy. Since httpClient object is object inside the class method, how do i mock this object in groovy test case?

This is the test case that I have so far.I am trying to mock to null, but not happening...

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)

    }   
}

解决方案

You can use

HttpClient.metaClass.executeMethod = {Type name -> doSomething}

You need to declare the closure signature with the correct Type i.e. String, Map, etc.

void testSendMessage(){
    def serviceUrl = properties.getProperty("ITEM").toString()

    // mocking to return null   
    HttpClient.metaClass.executeMethod = {HttpMethod str -> null}
    def responseXml = helper.message(serviceUrl)

}

这篇关于单元从Groovy测试用例中测试Java类中的静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆