简体   繁体   中英

Wanted but not invoked: Mockito and Powermock

I have a Junit test that I inherited that is no longer working. It is using PowerMock 1.4.12, Mockito 1.9.0 and Junit 4.8.2. It was working awhile back but stopped and I am trying to get it to work again.

Wanted but not invoked:
clerkReviewPackageHelper.addSubmissionQueue(
    <any>,
    <any>,
    <any>,
    <any>
);
-> at icis.cr.approvefilingdetail.CRFilingToQueuesActionTest.test_post_handled_add_submission_queue(CRFilingToQueuesActionTest.java:47)
Actually, there were zero interactions with this mock.

    at icis.cr.approvefilingdetail.CRFilingToQueuesActionTest.test_post_handled_add_submission_queue(CRFilingToQueuesActionTest.java:47)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:592)
    at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:66)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:312)
    at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:86)
    at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:94)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:296)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:284)
    at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:84)
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:209)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:148)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:122)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:102)
    at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
    at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:42)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Here is the code for this test:

@Test
public void test_post_handled_add_submission_queue() throws Exception {
    when(request.getMethod()).thenReturn(BaseCRAction.POST);
    when(sessionInfo.getSubmissionId()).thenReturn(SUBMISSION_ID);
    when(crFilingToQueuesForm.getAction()).thenReturn(null);
    crFilingToQueuesAction.executeProcess(actionMapping, crFilingToQueuesForm, request, response);
    verify(clerkReviewPackageHelper, times(0)).removeSubmissionQueue(null);
    **verify(clerkReviewPackageHelper).addSubmissionQueue(any(String.class), any(String.class), any(String.class), any(String.class));**
    verify(actionMapping).findForward(eq(BaseCRAction.FORWARD_SUCCESS_REDIRECT));
}

And this is the method that it calls from the line in the exception:

public void addSubmissionQueue(String submissionId, String queueId, String reviewOn, String employeeId) throws BadDBConnection {
Session session = injector.inject();
session.executeNonSelectingCall(clerkReviewPackage.addQueue(submissionId, queueId, reviewOn, employeeId));
session.release();
}

Any help would be appreciated!

Thanks,

Tom

It looks like executeProcess used to call addSubmissionQueue but doesn't anymore, but without the code of executeProcess it's hard to say for sure.

If it's appropriate not to call addSubmissionQueue , you can simply delete the reference to that verification line. If it is important to call it, or call it under specific circumstances, edit the test so the verify line only happens in test cases where a call to that method is required.

Note that because Mockito is involved, your test isn't calling the actual code for addSubmissionQueue , but a mock instead. Read more about how Mockito works in the examples on the Mockito homepage .

Try :

import org.mockito.Mockito;

// ...

Mockito.verify(clerkReviewPackageHelper, Mockito.times(0)).addSubmissionQueue();

Your clerkReviewPackageHelper mock doesn't seem to be associated with your call to executeProcess in any way. It's not passed in, and it doesn't seem to be returned, directly or indirectly, from any method calls on any mocks that are passed in to executeProcess . It therefore hardly seems reasonable to expect one of its methods to get called by executeProcess .

What you need to do is look through the call to executeProcess and find the object, if any, on which addSubmissionQueue is called. Without seeing the code for executeProcess , I can't really offer assistance with this. It may be but there is no such method call, as @JeffBowman has conjectured in his answer. But there are also other possibilities.

  • If the object on which addSubmissionQueue is called is one of the parameters to executeProcess , then you should use clerkReviewPackageHelper as the corresponding argument in the test.
  • If the object on which addSubmissionQueue is called is an a field in the one of the parameters to the executeProcess , then inject clerkReviewPackageHelper , either in the constructor of crFilingToQueuesAction , or a setter, or even with @InjectMocks .
  • If the object on which addSubmissionQueue is called is obtained from a method call on one of the other parameters to executeProcess , then you need to make that call return clerkReviewPackageHelper . This might mean injecting that value into some object, or it might mean stubbing a method call on a mock. For example, if the method that you're testing obtains clerkReviewPackageHelper by calling a line like crFilingToQueuesForm.getHelper() , then you need to write when(crFilingToQueuesForm.getHelper()).thenReturn(clerkReviewPackageHelper); or something similar. It may turn out to be more complex than this of course. I can't tell without seeing your code.

If you need more help, then please post the code of your executeProcess method.

我在为控制器方法编写测试用例时也遇到了这个问题,我错过了将参数声明给我的测试方法的问题,这些参数是在setup方法中声明但未初始化的,所以我在setup方法中做了这些操作,并解决了问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM