I am facing an issue when run Mockito
Unit
test with @Spy
for Groovy
class which extends an abstract
class. This error does not occur if I remove abstract
or create a parent class as a regular Java
class. What might be the issue?
abstract class ClassA
{
}
@Component
class ClassB extends ClassA
{
void validateBeforeCreate(String arg1, Object arg2)
{
check(arg1, arg2)
}
protected void check(String arg1, Object arg2)
{
// some validation logic
}
}
@ExtendWith(MockitoExtension)
class ClassBTest
{
@Spy
@InjectMocks
private ClassB classB
@Test
void testValidateBeforeCreate_Success()
{
String arg1 = "test"
Object arg2 = new Object()
doNothing().when(classB).check(arg1, arg2)
assertDoesNotThrow({ classB.validateBeforeCreate(arg1, arg2) } as Executable)
}
}
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:146)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.createCallSite(CallSiteArray.java:163)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:115)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:135)
at com.tr.cws.workflow.trigger.test.ClassBTest.testValidateBeforeCreate_Success(ClassBTest.groovy:25)