Tôi đang sử dụng Mockito 1.9 với Grails 1.3.7 và tôi có một lỗi lạ.Lỗi trong Mockito với Grails/Groovy
Các trường hợp kiểm tra sau trong java hoạt động:
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(){
}
}
}
thử nghiệm trong groovy này không hoạt động:
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(){
}
}
Đây là thông báo lỗi:
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)
Liệu anymone quan sát cùng một lỗi?
Tôi nên sử dụng gì sau đó? Tôi có cùng một vấn đề và tôi bị mắc kẹt với nó. – Guillaume
Bạn có thể thử [mockito-groovy-support] (https://github.com/cyrusinnovation/mockito-groovy-support). Nó giải quyết vấn đề getMetaClass() cho tôi – csab