How to use Mockito BDD to mock a Class<?> return type method?

47 views
Skip to first unread message

Jiaqian Sun

unread,
May 31, 2023, 2:06:16 AM5/31/23
to mockito
I'm having trouble to use Mockito BDD to mock the method whose return type is Class<?>

I'm trying to use BDD to mock this method:

public Class<?> getInterfaceClass() {
if (this.interfaceClass == null) {
try {
this.interfaceClass = this.getClass().getClassLoader().
loadClass(this.interfaceType);
} catch (ClassNotFoundException var2) {
throw new RuntimeException(var2);
} catch (NullPointerException var3) {
return null;
}
}

return this.interfaceClass;
}

This is how I mocked:

given(serviceFactoryBean.getInterfaceClass()).willReturn
(Comparable.class);


But I got this error: Cannot resolve method 'willReturn(Class<Comparable>)'

May I know how to resolve this problem?


I also post this question in the stackoverflow. https://stackoverflow.com/questions/76350341/how-to-use-mockito-bdd-to-mock-a-class-return-type-method


Reply all
Reply to author
Forward
0 new messages