Mockito和Grails / Groovy中的Bug [英] Bug in Mockito with Grails/Groovy

查看:990
本文介绍了Mockito和Grails / Groovy中的Bug的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在java中的以下测试用例工作:

  import static org.mockito.Mockito。*; 

public class MockitoTests extends TestCase {

@Test
public void testSomeVoidMethod(){
TestClass spy = spy(new TestClass());
doNothing()。when(spy).someVoidMethod();


public static class TestClass {

public void someVoidMethod(){
}
}
}

这个在groovy中的测试不起作用:

  import static org.mockito.Mockito。* 

public class MockitoTests extends TestCase {
$ b $ public void testSomeVoidMethod( ){
def testClassMock = spy(new TestClass())
doNothing()。when(testClassMock).someVoidMethod()
}

}

public class TestClass {

public void someVoidMethod(){
}
}

这是错误信息:

 只有void方法可以做(没有! 
正确使用doNothing()的例子:
doNothing()。
doThrow(new RuntimeException())
.when(mock).someVoidMethod();
上面的意思是:
someVoidMethod()第一次不做任何事,但第二次抛出一个异常叫做
org.mockito.exceptions.base.MockitoException:
只有void方法可以没做什么()!
正确使用doNothing()的例子:
doNothing()。
doThrow(new RuntimeException())
.when(mock).someVoidMethod();
上面的意思是:
someVoidMethod()第一次不做任何事情,但第二次抛出异常在org.codehaus.groovy.runtime.callsite.CallSiteArray.createPogoSite中调用
(CallSiteArray.java :129)
在org.codehaus.groovy.runtime.callsite.CallSiteArray.createCallSite(CallSiteArray.java:146)
在org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java :40)
org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java :120)

是否有人观察到同样的错误?

解决方案

问题是Groovy在它到达 someVoidMethod 之前拦截您的方法调用。实际被调用的方法是 getMetaClass ,它不是一个无效方法。



你可以通过替换:

  doNothing()。when(testClassMock).someVoidMethod()

包含:

  doReturn(testClassMock.getMetaClass()) .when(testClassMock).someVoidMethod()

我不确定您能够绕过此问题使用库存 Mockito 和Groovy。


I am using Mockito 1.9 with Grails 1.3.7 and I have a strange bug.

The following test case in java works:

import static org.mockito.Mockito.*;

public class MockitoTests extends TestCase {

    @Test
    public void testSomeVoidMethod(){
        TestClass spy = spy(new TestClass());
        doNothing().when(spy).someVoidMethod();
    }

    public static class TestClass {

        public void someVoidMethod(){
        }
    }
}

This test in groovy does not work:

import static org.mockito.Mockito.*

public class MockitoTests extends TestCase {

    public void testSomeVoidMethod() {
        def testClassMock = spy(new TestClass())
        doNothing().when(testClassMock).someVoidMethod()
    }

}

public class TestClass{

    public void someVoidMethod(){
    }
}

This is the error message:

only void methods can doNothing()!
Example of correct use of doNothing():
    doNothing().
    doThrow(new RuntimeException())
    .when(mock).someVoidMethod();
Above means:
someVoidMethod() does nothing the 1st time but throws an exception the 2nd time is called
org.mockito.exceptions.base.MockitoException: 
Only void methods can doNothing()!
Example of correct use of doNothing():
    doNothing().
    doThrow(new RuntimeException())
    .when(mock).someVoidMethod();
Above means:
someVoidMethod() does nothing the 1st time but throws an exception the 2nd time is called
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.createPogoSite(CallSiteArray.java:129)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.createCallSite(CallSiteArray.java:146)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:40)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120)

Does anymone observed the same error?

解决方案

The problem is Groovy is intercepting your method call before it reaches someVoidMethod. The method actually being called is getMetaClass which is not a void method.

You can verify this is happening by replacing:

doNothing().when(testClassMock).someVoidMethod()

with:

doReturn(testClassMock.getMetaClass()).when(testClassMock).someVoidMethod()

I'm not sure you will be able to get around this issue using stock Mockito and Groovy.

这篇关于Mockito和Grails / Groovy中的Bug的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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