如何解决使用Groovy类的@Spy编写Mockito单元测试时出错,该类扩展了抽象类
当对Mockito
类使用Unit
运行@Spy
Groovy
测试来扩展abstract
类时,我遇到一个问题。如果删除abstract
或将父类创建为常规Java
类,则不会发生此错误。可能是什么问题?
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)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。