gpt4 book ai didi

java - Powermock 无法模拟 MysqlConnect

转载 作者:行者123 更新时间:2023-11-30 07:12:54 25 4
gpt4 key购买 nike

这是我的 Mysql 类:

public class MysqlConnection {

public Connection MysqlConnect() {
String url = "jdbc:mysql://localhost:3306/";
String dbName = "dashboard";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String passwd = "";
Connection conn = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url+dbName,userName,passwd);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
}

而我的 PowerMockito 测试类是这样的:

@RunWith(PowerMockRunner.class)
@PowerMockIgnore("javax.management.*")
@PrepareForTest(Class.class)
public class MysqlConnectionTest {

@Test(expected = ClassNotFoundException.class)
public void MysqlConnectiontest() {
PowerMockito.mockStatic(Class.class);
try {
when(Class.forName(Mockito.anyString())).thenThrow(new ClassNotFoundException("error message"));
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
obj.MysqlConnect();
}
}

错误的Stacktrace是:

java.lang.AssertionError: Expected exception: java.lang.ClassNotFoundException
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:314)
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.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:112)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:73)
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:50)
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)

该程序没有模拟任何代码行,而是执行实际 MySQLConnect 函数中的代码。它正在使用实际的驱动程序。如果我从 @Test 中删除预期的部分,则测试成功。也就是说,不进行模拟,加载驱动程序和一切。

最佳答案

您不能使用 PowerMockito 或任何其他依赖运行时字节代码修改的模拟框架来模拟 java.lang.Class。这样做的原因是当 mockito 代理正在操纵(检测)应该模拟的类时,这些类已经加载。

你为什么不模拟你的 MysqlConnection 类?您可以在那里监视您的 MysqlConnect 方法并返回一个模拟连接。

关于java - Powermock 无法模拟 MysqlConnect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19948014/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com