被抛出的Mockito和PowerMock MethodFoundException



使用PowerMockito和Mockito为连接池构建一些简单的单元测试时,我制作了我制作的一些简单的单元测试,我制作了围绕Hikari CP的连接池。测试的设置如下。让我感到困惑的是,我有少数几个未显示的单元测试,并且它们都使用相同的设置和方法通过。只是这个单位测试继续失败。而且我在顶部放置了什么when语句都没关系,他们都找不到方法。

org.powermock.reflect.exceptions.MethodNotFoundException: No methods matching the name(s) getColumnCount were found in the class hierarchy of class java.lang.Object.
at org.powermock.reflect.internal.WhiteboxImpl.getMethods(WhiteboxImpl.java:1720)
at org.powermock.reflect.internal.WhiteboxImpl.getMethods(WhiteboxImpl.java:1745)
at org.powermock.reflect.internal.WhiteboxImpl.getBestMethodCandidate(WhiteboxImpl.java:983)
at org.powermock.core.MockGateway$MockInvocation.findMethodToInvoke(MockGateway.java:317)
at org.powermock.core.MockGateway$MockInvocation.init(MockGateway.java:356)
at org.powermock.core.MockGateway$MockInvocation.<init>(MockGateway.java:307)
at org.powermock.core.MockGateway.doMethodCall(MockGateway.java:142)
at org.powermock.core.MockGateway.methodCall(MockGateway.java:125)
at com.datafiniti.utils.mysqlconnpool.MysqlConnPoolTests.executeStringQuery(MysqlConnPoolTests.java:149)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:316)
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:300)
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:288)
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:208)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:147)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:121)
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:123)
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:53)
at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

这是单元测试:

@RunWith(PowerMockRunner.class)
public class MysqlConnPoolTests {
private HikariConfig mockHikariConfig;
private HikariDataSource hikariDataSource;
@Before
@PrepareForTest({HikariConfig.class, HikariDataSource.class})
public void beforEachTest() throws Exception {
    // mock the hikari confic used within the connection pool
    mockHikariConfig = PowerMockito.mock(HikariConfig.class);
    // mock hikari config constructor
    PowerMockito.whenNew(HikariConfig.class)
            .withNoArguments()
            .thenReturn(mockHikariConfig);
    // mock hikari config data source setting
    PowerMockito.doNothing().when(mockHikariConfig)
            .setDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource");
    // mock the hikari data source used within the connection pool
    hikariDataSource = PowerMockito.mock(HikariDataSource.class);
    // mock hikari data source constructor
    PowerMockito.whenNew(HikariDataSource.class)
            .withParameterTypes(HikariConfig.class)
            .withArguments(Mockito.isA(HikariConfig.class))
            .thenReturn(hikariDataSource);
}
@Test
@PrepareForTest({MysqlConnectionPool.class, Connection.class, Statement.class, ResultSet.class, ResultSetMetaData.class})
public void executeStringQuery() throws Exception {
    PowerMockito.spy(MysqlConnectionPool.class);
    Connection mockConnection = Mockito.mock(Connection.class);
    Statement mockStatement = Mockito.mock(Statement.class);
    ResultSet mockResultSet = Mockito.mock(ResultSet.class);
    ResultSetMetaData mockMeta = Mockito.mock(ResultSetMetaData.class);
    Mockito.when(mockMeta.getColumnCount()).thenReturn(1);
    Mockito.when(mockMeta.getColumnLabel(1)).thenReturn("foo");
    Mockito.when(mockResultSet.first()).thenReturn(true);
    Mockito.when(mockResultSet.next()).thenReturn(false);
    Mockito.when(mockResultSet.getString(1)).thenReturn("bar");
    Mockito.when(mockStatement.execute(Mockito.eq("SELECT * FROM table;"))).thenReturn(true);
    Mockito.when(mockStatement.getResultSet()).thenReturn(mockResultSet);
    Mockito.when(mockConnection.createStatement()).thenReturn(mockStatement);
    Mockito.when(hikariDataSource.getConnection()).thenReturn(mockConnection);
    MysqlConnectionPool pool = new MysqlConnectionPool().create();
    List<Map<String, String>> result = pool.executeQuery("SELECT * FROM table;");
    assertEquals(1, result.size());
    assertTrue(result.get(0).containsKey("foo"));
    assertEquals("bar", result.get(0).get("foo"));
}

}

要发布答案,以防万一其他人看到此答案。将所有版本恢复为1.6.5解决了问题。

我没有足够的声誉来发表评论,所以不幸的是我使用答案空间来评论。

Markll,我遇到了类似的问题,这是我的问题-ORG.Powermock.reflect.exceptions.methodnotfoundexception: - 嘲笑Java SQL类时dis ococto.时(operconnection.createStatement())。为你工作?我的甚至没有超越这一点。也许你可以帮助我。我的最新结论是因为连接,语句和结果集实际上是界面而不是类,这就是引发错误的原因,说它在对象的层次结构中找不到。

相关内容

  • 没有找到相关文章

最新更新