从Groovy测试用例中嘲笑Java类 [英] Mocking Java Class from Groovy Test case

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

问题描述

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



下面是Java类
//这个帮助类是一个java类

  public class Helper {

public static message(final String serviceUrl ){
----------一些代码--------

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

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



我写的测试用例在groovy到目前为止:

  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)

}
}

关于它为什么没有正确模拟的任何想法。
Advance Advance

解决方案

好!
测试静态方法非常困难,除非您将它们声明为属性,否则更难测试局部变量。我关于静态类的结论有时是关于设计的,因为你可以把这块代码放在其他地方并重用。无论如何,这里是我的方法来测试没有,存根,模拟和MOP这种情况:

这是一个Java类,像你的类:

  import java.text.SimpleDateFormat; 
import java.text.ParseException;
import java.util.Date;
$ b $ public class Helper {

public static String message(final String serviceUrl)throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat(dd-MM-yy) ;
日期d = formatter.parse(serviceUrl);
String code = d.toString();
返回码;




$ b $这些是我的GroovyTestCase:

  import groovy.mock.interceptor.MockFor 
import groovy.mock.interceptor.StubFor
import java.text.SimpleDateFormat
$ b $ class TestHelper扩展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


$ b $ 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 testSendMessageWithMO​​P(){
SimpleDateFormat.metaClass.parse = {字符串s - >
printlnMOP
new Date()+ 1

def formatter = new SimpleDateFormat()
println formatter.parse(hello world!)
printlnExe:+ new Helper()。message(01-01-12)
}
}

您的问题的答案可能是:因为是方法中的局部变量,并且不是测试的协作者。



问候


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 method in it where a HttpClient object is obtained and executeMethod is called on it. I am trying to mock this httpClient.executeMethod() in groovy test case, but not able to mock it right.

Below is the Java class //this helper class is a java class

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

}
}

The test case that i have written in groovy so far is:

    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)

            }   
        }

Any ideas on why it is not mocking correctly. Advance Thanks

解决方案

Well! Is very hard to test static methods, and even harder to test local variables unless you declare them as properties. My conclusion about the static classes sometimes is about design, because you can put that block of code in other place and reuse. Anyway, here is my approach to test with nothing, with stub, with a mock and with MOP this case:

This a Java class like your class:

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

And this is my 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")
  }
}

The answer to your question maybe is: because is a local variable in a method, and that is not a collaborator to test.

Regards

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

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