2012-02-21 8 views
5

Grails 1.3.7 ile Mockito 1.9 kullanıyorum ve garip bir hatam var.Greys/Groovy ile Mockito içinde hata

java aşağıdaki test durumu çalışır:

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 Bu test çalışmaz:

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) 

anymone gözlenen mu:

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

Bu hata mesajı aynı hata?

cevap

8

Sorun, Groovy'nin someVoidMethod ulaşmadan önce yöntem çağrınızı durdurmasıdır. Aslında çağrılan yöntem, geçersiz bir yöntem olmayan getMetaClass'dur.

Bu değiştirerek oluyor doğrulayabilirsiniz:

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

ile:

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

Sana stok Mockito ve Groovy kullanarak bu sorunu çözmek mümkün olacaktır emin değilim.

+0

Ne kullanmalıyım? Bende aynı sorun var ve buna bağlı kaldım. – Guillaume

+0

[mockito-groovy-support] 'yu deneyebilirsiniz (https://github.com/cyrusinnovation/mockito-groovy-support). GetMetaClass() sorununu benim için çözdü – csab