我想使用Mockito(和PowerMockito,如果需要)测试我的DAO方法,但我不知道如何执行此操作。调用静态方法的最大问题(MySQLStationDAO中的MySQLDAOFactory.getConnection())。你可以帮我吗?使用Mockito和PowerMockito进行DAO测试
我取得联系是这样的:
public class MySQLDAOFactory extends DAOFactory {
public static Connection getConnection() throws DAOException {
Connection con = null;
try {
con = getDataSource().getConnection();
} catch (SQLException e) {
throw new DAOException(Messages.CANNOT_OBTAIN_CONNECTION, e);
}
return con;
}
这里是一个DAO方法:
public class MySQLStationDAO implements StationDAO {
@Override
public List<Station> getAllStations() throws DAOException {
List<Station> stations = new ArrayList<>();
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
con = MySQLDAOFactory.getConnection();
stmt = con.createStatement();
rs = stmt.executeQuery(MySQLQueries.SQL_GET_ALL_STATIONS);
while (rs.next()) {
stations.add(extractStation(rs));
}
} catch (SQLException e) {
throw new DAOException(Messages.CANNOT_OBTAIN_ALL_STATIONS, e);
} finally {
MySQLDAOFactory.close(con, stmt, rs);
}
return stations;
}
究竟是什么问题?你有例外吗? – BetaRide
我们可能还需要一些额外的信息。你的DataSource在哪里配置? – Nitax