问题
I am developing Test
classes using PowerMock, as all my Legacy
code is written using Private Static methods().
When running the test class, I am getting below error:
org.powermock.reflect.exceptions.MethodNotFoundException: No method found with name 'insertPaytmBilling' with parameter types: [ java.util.LinkedList ] in class com.xyz.PaytmBilling.
at org.powermock.reflect.internal.WhiteboxImpl.throwExceptionIfMethodWasNotFound(WhiteboxImpl.java:1122)
at org.powermock.reflect.internal.WhiteboxImpl.findMethodOrThrowException(WhiteboxImpl.java:913)
at org.powermock.reflect.internal.WhiteboxImpl.doInvokeMethod(WhiteboxImpl.java:807)
at org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:675)
at org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:401)
at org.powermock.api.mockito.internal.expectation.PowerMockitoStubberImpl.when(PowerMockitoStubberImpl.java:94)
at safe.billing.MemberBillingTest.testMemberBilling(MemberBillingTest.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:326)
at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:89)
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:97)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:310)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:131)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.access$100(PowerMockJUnit47RunnerDelegateImpl.java:59)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner$TestExecutorStatement.evaluate(PowerMockJUnit47RunnerDelegateImpl.java:147)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.evaluateStatement(PowerMockJUnit47RunnerDelegateImpl.java:107)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:298)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:87)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:50)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:218)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:160)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:134)
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:136)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:121)
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:57)
at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
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:538)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
Method:
private static void insertpaytmBilling(ArrayList allPaytmBill) throws Exception{
Connection conn = getConnection(userId, passwd, url);
PreparedStatement pStatement = conn.prepareStatement(DATA_SQL);
for (int i=0; i<allPaytmBill.size(); i++){
MemberBill mBill = (MemberBill) allPaytmBill.get(i);
pStatement.setString(1, mBill.getMemberId());
......
......
..........
pStatement.execute();
//pStatement.addBatch();
}
pStatement.close();
conn.close();
}
Test class:
@RunWith(PowerMockRunner.class)
@PrepareForTest({ DriverManager.class, ABCHandler.class })
@PowerMockIgnore("javax.management.*")
public class PaytmBillingTest {
@Mock
private static Connection conn;
List<paytmBill> paytmBillings = null;
@Before
public void beforeTest() {
paytmBillings = new ArrayList<>();
paytmBill m = new paytmBill();
....
....
paytmBillings.add(m);
}
@Test
public void testpaytmBilling() throws Exception {
// Setup
Connection conn = PowerMockito.mock(Connection.class);
PowerMockito.mockStatic(DriverManager.class);
PowerMockito.mockStatic(ABCHandler.class);
PowerMockito.mockStatic(PreparedStatement.class);
// Create Spy
paytmBilling spy = PowerMockito.spy(new paytmBilling());
BDDMockito.given(ABCHandler.getConnection(anyString(), anyString(), anyString())).willReturn(conn);
PowerMockito.doReturn(true).when(spy, "insertpaytmBilling", anyList());
}
}
Edit-1:
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers!
3 matchers expected, 1 recorded:
-> at safe.billing.MemberBillingTest.testMemberBilling(MemberBillingTest.java:76)
This exception may occur if matchers are combined with raw values:
//incorrect:
someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
//correct:
someMethod(anyObject(), eq("String by matcher"));
回答1:
You are missing paytmBilling
class in PrepareForTest
annotation to spy on:
@PrepareForTest({ DriverManager.class, ABCHandler.class , paytmBilling.class)
回答2:
There are two issues. First with the parameter type. The fix was suggested in the comments:
be more specific, replace anyList() with any(ArrayList.class)
When you use a construction like:
PowerMockito.doReturn(true).when(spy, "insertpaytmBilling", anyList())
PowerMock is looking for a method with the name "insertpaytmBilling" in spy's class with a list of parameters assignable to the given list of parameters. Looks like the issue in your case that anyList()
by default returns an instance of LinkedList
which is not assignable to a real parameter of the method ArrayList
.
The second issue (Edit-1) is that Mockito does not support and cannot support mocking with using matches and raw parameters together. You should always use either matcher like anyString()
or parameter Some string
. As I see from the code snippet and stack trace the exception is thrown in another part of the test.
来源:https://stackoverflow.com/questions/52052648/org-powermock-reflect-exceptions-methodnotfoundexception-no-method-found-with-n