1

I am pretty new to Mockito and Spring. I am trying to mock many methods of many classes. I want to create a functionality where bean name and method name can given as input as string, and it will throw same exception.

Example I have a class A and Class B

@Named
public Class A {
   public void methodInA() {
   System.out.println("In A"); 
  }
}

@Named
public Class B {
   public void methodInB() {
   System.out.println("In B"); 
  }
}


Below is what I am trying to do in test.

TestConfig:

@SpyBean
A a;

Test::

String className = "A";
String methodName = "methodInA";


Object bean = applicationContext.getBean("a");
Class clazzToSpy = bean.getClass();

Class[] paramTypes = clazzToSpy.getMethod(methodName);

Answer answer = new Answer() {
@Override
public Object answer(final InvocationOnMock invocationOnMock) throws Throwable {
   if(invocationOnMock.getMethod().getName().equals(methodName)) {
       throw new UnknownError("Manual Exception Created");
   }
   return invocationOnMock.callRealMethod();

  }
};

//Here I want to use the answer on the mock Something like below.

Method mockedMethod = clazzToSpy.getMethod(methodName, paramTypes);

Mockito.doAnswer(answer).when(mockedMethod)


I understand there is a gap in my understanding. How do I achieve something like above?

Has someone tried doing similar thing?

Related threads which I have tried:

  1. Mocking Reflection based calls

  2. Mocking getClass method with PowerMockito

  3. mockito : mock method call with parameters by reflection

  4. Mockito: is it possible to combine mock with a method name to create a methodCall inside a when() call?

  5. Using Mockito to mock methods by reflection

Anchika Agarwal
  • 560
  • 1
  • 6
  • 18
  • How exactly does it not work? A compile error since [`Class.getMethod(...)`](https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/lang/Class.html#getMethod(java.lang.String,java.lang.Class...)) returns `Method`, of course, rather than `Class[]`? (And, BTW, `Error` ← `VirtualMachineError` („Thrown to indicate that the Java Virtual Machine is broken or has run out of resources necessary for it to continue operating.“) ← `UnknownError` reads: „Thrown when an unknown but serious exception has occurred in the Java Virtual Machine.“ This is surely not what you want to throw yourself.) – Gerold Broser Jun 12 '20 at 22:18
  • Hi GeroldBroser, this gives me unfinished stubbing exception. – Anchika Agarwal Jun 13 '20 at 06:35

1 Answers1

0

This seems not going to work since when you:

@Spy
Method methodSpy;

Mockito throws:

org.mockito.exceptions.base.MockitoException: Unable to initialize @Spy annotated field 'methodSpy'.
Please ensure that the type 'Method' has a no-arg constructor.

and if you:

@Mock
Method methodMock;

it throws:

org.mockito.exceptions.base.MockitoException:
Cannot mock/spy class java.lang.reflect.Method
Mockito cannot mock/spy because :
    - final class

at:

MockitoAnnotations.initMocks( this );
Gerold Broser
  • 14,080
  • 5
  • 48
  • 107