我的目标是模拟静态方法
DriverManager.getConnection(String, String, String)
使用Mockito和PowerMock。
我正在执行的代码如下:
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import java.sql.DriverManager;
import java.sql.SQLException;
import static org.mockito.Matchers.any;
import static org.mockito.BDDMockito.given;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
@RunWith(PowerMockRunner.class)
@PrepareForTest({DriverManager.class})
public class SqlHelperConnectionTest {
@org.junit.Test
public void testSqlConnectionSQLException() throws Exception {
mockStatic(DriverManager.class);
given(DriverManager.getConnection(any(String.class), any(String.class), any(String.class))).willThrow(new SQLException("ERROR test"));
DriverManager.getConnection("","","");
}
}
我得到的错误如下:
java.lang.ClassCastException: org.mockito.internal.matchers.LocalizedMatcher cannot be cast to org.mockito.ArgumentMatcher
我使用的依赖项如下:
testCompile group: 'junit', name: 'junit', version: '4.10'
testCompile 'org.mockito:mockito-core:1.10.19'
testCompile 'org.powermock:powermock-module-junit4:1.6.4'
testCompile 'org.powermock:powermock-api-mockito:1.6.4'
我使用的版本是this post上描述的版本。
我很感激任何提示或暗示。
提前多多感谢!