org.powermock.reflect.exceptions.MethodNotFoundException:找不到名称为“”且参数类型为:[java.util.LinkedList]的方法

时间:2018-08-28 07:47:05

标签: java junit powermock powermockito

我正在使用 PowerMock 开发Test类,因为我所有的Legacy代码都是使用 Private Static 方法编写的。

运行测试类时,出现以下错误:

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)

方法:

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();
}

测试课程:

@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());

    }
}

编辑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"));

2 个答案:

答案 0 :(得分:0)

您缺少paytmBilling注释中的PrepareForTest类来监视:

@PrepareForTest({ DriverManager.class, ABCHandler.class , paytmBilling.class)

答案 1 :(得分:0)

有两个问题。首先是参数类型。该修复建议在注释中提出:

  

更具体地说,将anyList()替换为any(ArrayList.class)

当您使用类似

的结构时
PowerMockito.doReturn(true).when(spy, "insertpaytmBilling", anyList())

PowerMock正在间谍类中寻找一种名称为“ insertpaytmBilling”的方法,该方法具有可分配给给定参数列表的参数列表。看来您遇到的问题是,anyList()默认返回一个LinkedList的实例,该实例不可分配给方法ArrayList的实参。

第二个问题(Edit-1)是Mockito不支持也不支持通过同时使用match和raw参数进行模拟。您应该始终使用anyString()之类的匹配器或参数Some string。正如我从代码片段和堆栈跟踪中看到的那样,该异常在测试的另一部分中引发。