简体   繁体   English

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

[英]org.powermock.reflect.exceptions.MethodNotFoundException: No method found with name '' with parameter types: [ java.util.LinkedList ]

I am developing Test classes using PowerMock , as all my Legacy code is written using Private Static methods(). 我正在使用PowerMock开发Test类,因为我的所有Legacy代码都是使用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: 编辑-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"));

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

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

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) 更具体地说,用any(ArrayList.class)替换anyList()

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. PowerMock正在间谍类中寻找一种名称为“ insertpaytmBilling”的方法,该方法具有可分配给给定参数列表的参数列表。 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 . 在您的情况下看起来像这样的问题:默认情况下, anyList()返回一个LinkedList实例,该实例不可分配给ArrayList方法的真实参数。

The second issue (Edit-1) is that Mockito does not support and cannot support mocking with using matches and raw parameters together. 第二个问题(Edit-1)是Mockito不支持也不支持通过同时使用match和raw参数进行模拟。 You should always use either matcher like anyString() or parameter Some string . 您应该始终使用匹配器(如anyString()或参数Some string As I see from the code snippet and stack trace the exception is thrown in another part of the test. 正如我从代码片段和堆栈跟踪中看到的那样,该异常在测试的另一部分中引发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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