2012-02-21 20 views
5

Grails 1.3.7でMockito 1.9を使用しています。私は奇妙なバグがあります。グルーヴィーでGrails/Groovyを使用したMockitoのバグ

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(){ 
     } 
    } 
} 

このテストは動作しません:

Javaで次のテストケース働く

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(){ 
    } 
} 

これはエラーメッセージです:anymoneは

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) 

認められません同じエラー?

答えて

8

Groovyは、someVoidMethodに達する前にメソッド呼び出しをインターセプトしています。実際に呼び出されるメソッドはvoidメソッドではないgetMetaClassです。

あなたは置き換えることによって、これが起こっているかどうかを確認することができます:私はあなたが株式MockitoとGroovyを使用してこの問題を回避することができるようになりますわからない

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

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

をして。

+0

それでは、どうすればよいですか?私は同じ問題を抱えており、私はそれに固執しています。 – Guillaume

+0

[mockito-groovy-support](https://github.com/cyrusinnovation/mockito-groovy-support)を試すことができます。私のためにgetMetaClass()の問題を解決しました – csab

関連する問題