我正在尝试为一个名为DriverManager.getConnection()
的方法编写单元测试。我正在使用PowerMock 1.5和easymock。不知何故,Powermock无法模拟DriverManager,最终调用了真正的DriverManager。这是我的代码的样子:-
@Test
public void checkConnection() {
try {
String url = "jdbc:oracle:thin:@//myhost:1521/orcl";
String password = "@55";
String dbName = "Halloween";
String username = "Dracula";
PowerMock.mockStatic(DriverManager.class);
Connection connection = EasyMock.createMock(Connection.class);
List<Datasource> allDs = new ArrayList<Datasource>(2);
Datasource d1 = new Datasource();
d1.setUrl(url);
allDs.add(d1);
allDs.add(new Datasource());
EasyMock.expect(hibernateTemplate.find("from Datasource ds where ds.dsName = ?", dbName)).andReturn(allDs);
EasyMock.expect(DriverManager.getConnection(d1.getUrl(), username, password)).andReturn(connection);
PowerMock.replay(DriverManager.class);
EasyMock.replay(hibernateTemplate);
Connection con = testee.authenticateUserForDatabase(dbName, username, password);
Assert.assertNotNull(con);
PowerMock.verify(DriverManager.class);
EasyMock.verify(hibernateTemplate);
} catch (Exception e) {
Assert.fail(e.getMessage());
}
}
我的类代码如下所示:-
@Override
public Connection getConnection(String dbName, String username, String password) {
Connection connection = null;
@SuppressWarnings("unchecked")
List<Datasource> ds = hibernateTemplate.find("from Datasource ds where ds.dsName = ?", dbName);
if (ds == null || ds.isEmpty()) {
throw new ProviderException("Invalid datasource name [" + dbName + "]");
}
Datasource d = (Datasource) ds.get(0);
int retryCount = 0;
boolean connected = false;
while (retryCount < 2 && !connected) {
try {
connection = DriverManager.getConnection(d.getUrl(), username, password);
LOG.info("Connected successfully to [{}]", dbName);
connected = true;
} catch (SQLException e) {
LOG.warn("Error: [{}] occured. Going to retry, attempt # [{}]", e.getMessage(), (retryCount + 1));
retryCount++;
connected = false;
}
}
return connection;
}
你可以改用javax.sql.DataSource,而不是使用DriverManager。
http://docs.oracle.com/javase/6/docs/api/javax/sql/DataSource.html
这样,您正在使用可以模拟的界面。如今,大多数应用程序都使用数据源接口,并且不会像这样与驱动程序管理器交互。
因此,您的代码可以具有如下所示的 api:
public class DataSourceFactory {
public DataSource getDataSource( String url, String usr, String pwd);
}
如果您愿意,您可以使用 DriverManager 实现真正的数据源,然后模拟 DataSourceFactory 进行单元测试。
如果您无法模拟静态调用DriverManager.getConnection
,一种可能的解决方法是跳过它。
将静态调用提取到 sut 中具有与静态方法相同的签名的受保护方法。
protected Connection driverManagerGetConnection(String url, String user, String password){
return DriverManager.getConnection(url, user, password);
}
现在,在您的测试中,使用 EasyMock 创建您的 sut 的部分模拟,模拟driverManagerGetConnection
方法。
public void testFoo{
// setup: sut
YourSutClass sut = EasyMock.createMockBuilder(YourSutClass.class)
.addMockedMethod("driverManagerGetConnection").createMock();
...
// setup: expectations
EasyMock.expect(sut.driverManagerGetConnection()).andReturn(mockConnection);
...
// exercise
EasyMock.replay(sut, mockConnection);
sut.whatever();
// verify
EasyMock.verify(sut, mockConnection);
}
使用此方法,唯一未测试的行是对静态方法的调用。