Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocking java.lang.reflect.Method using Mockito

I need to mock java.lang.reflect.Method for my test (I have dynamic things). We have written all the test cases using Junit spring & mockito. But I am facing issue while writing Junit for my Aspects. Also we have written custom annotation. Please help me on this

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring/services-test.xml")
@ImportAutoConfiguration(RefreshAutoConfiguration.class)
public abstract class AbsServicesTest extends AbstractJUnit4SpringContextTests {
    
    static{
        System.setProperty("LOCAL");
    }
}


public class TaxValidatorAspectTest extends AbsServicesTest {
    
    @Autowired
    private ResourcesUtils resourcesUtil;
    
    @Mock
    private JoinPoint joinPoint;
    
    @Mock
    private MethodSignature methodSignature;
    
    @Mock
    private Method method;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(new TaxValidatorAspectTest());
        Mockito.when(joinPoint.getSignature()).thenReturn(methodSignature);
        Mockito.when(methodSignature.getMethod()).thenReturn(method);
    }
    
    @Test
    public void testBeforeMethod() throws IOException{
        TaxValidatorAspect taxonomyValidationAspect = new TaxValidatorAspect();
        final Object[] arguments = new Object[4];
        arguments[0] = requiredParam1;
        arguments[1] = requiredParam2;
        arguments[2] = requiredParam3;
        Mockito.when(joinPoint.getArgs()).thenReturn(arguments);
        taxonomyValidationAspect.before(joinPoint);
    }
}

I am getting error like below

    org.mockito.exceptions.base.MockitoException: 
    Cannot mock/spy class java.lang.reflect.Method
    Mockito cannot mock/spy because :
     - final or anonymous class
        at com.taxvalidation.TaxValidatorAspectTest.setUp(TaxValidatorAspectTest.java:55)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
        at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
        at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
        at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
        at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
        at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
        at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
        at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
        at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
like image 983
dinu0101 Avatar asked Mar 07 '17 06:03

dinu0101


1 Answers

You can use below line to mock java.lang.reflect.Method:

Method mockMethod = this.getClass().getMethod(methodName);

It is not allowed to use mock or spy in straight forward way to create a mock object for java.lang.reflect.Method!

like image 117
San4musa Avatar answered Nov 09 '22 01:11

San4musa