I'm using Mockito (3.6.x) for my unit tests in Spring Boot (2.4.x) with JUnit 4. I need to test the next code block:
MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();
Method method = signature.getMethod();
CustomAnnotation customAnnotation = method.getAnnotation(CustomAnnotation.class);
I have tried this with other solutions (also with StackOverflow help), and I am working with PowerMockito now.
When I do:
MethodSignature methodSignature = PowerMockito.mock(MethodSignature.class);
PowerMockito.when((MethodSignature) proceedingJoinPointTest.getSignature()).thenReturn(methodSignature);
Method method = PowerMockito.mock(Method.class);
It works. But when I do:
CustomAnnotation customAnnotation = PowerMockito.mock(CustomAnnotation.class);
PowerMockito.when(method.getAnnotation(CustomAnnotation.class)).thenReturn(customAnnotation);
It fails. Also I tried with:
PowerMockito.when(method.getAnnotation(CustomAnnotation.class)).thenReturn(new CustomAnnotation() {
// Implementation
});
But it fails too. Do you have any idea to solve it?
Thank you soo much!