gpt4 book ai didi

java - Mockito——模拟数据库调用

转载 作者:搜寻专家 更新时间:2023-11-01 03:52:17 26 4
gpt4 key购买 nike

我尝试使用返回用户详细信息的 Mockito 模拟以下方法。

它正在对数据库进行实际调用,而不是返回模拟结果。

我的方法-

public User getUserById(String userId){
if (userId == null) {
throw new IllegalArgumentException("AssociateId cannot be null");
}
User user = new User();
preparedStatement = null;
try {
connection = dbConnection.openConnection(properties, inputStream);
query = queryReader
.getQuery(RelationshipManagerConstants.selectUser);
preparedStatement = connection.prepareStatement(query);
preparedStatement.setString(I_User.associateId, userId);
resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
user.setAssociateId(resultSet.getString(I_User.associateId));
user.setAssociatePassword(resultSet
.getString(I_User.associatePassword));
user.setAssociateRole(resultSet.getInt(I_User.associateRole));
user.setAssociateIsActive(resultSet
.getBoolean(I_User.associateIsActive));
user.setAssociateEmail(resultSet
.getString(I_User.associateEmail));
}
} catch (ClassNotFoundException e) {
LOGGER.warning("Cannot return User Details. ClassNotFoundException occured.");
} catch (SQLException e) {
LOGGER.warning("Cannot return User Details. SQLException occured.");
} catch (IOException e) {
LOGGER.warning("Cannot return User Details. IOException occured.");
} finally {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
LOGGER.warning("Failed to close resultSet.");
}
}
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
LOGGER.warning("Failed to close statement.");
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
LOGGER.warning("Failed to close connection.");
}
}
}
return user;
}

我的测试-

@Test
public void testGetUserById() throws Exception {
mockConnection = Mockito.mock(Connection.class);
Properties mockProperties = Mockito.mock(Properties.class);
InputStream mockInputStream = Mockito.mock(InputStream.class);
DBConnection mockDbConnection = Mockito.mock(DBConnection.class);
PreparedStatement mockPreparedStatement = Mockito.mock(PreparedStatement.class);
ResultSet mockResultSet = Mockito.mock(ResultSet.class);
QueryReader mockQueryReader = Mockito.mock(QueryReader.class);


PowerMockito.whenNew(DBConnection.class).withNoArguments()
.thenReturn(mockDbConnection);
PowerMockito.whenNew(QueryReader.class).withNoArguments()
.thenReturn(mockQueryReader);


String query = "select * from User where AssociateID=?;";
Mockito.when(mockDbConnection.openConnection(mockProperties, mockInputStream)).thenReturn(mockConnection);
Mockito.when(mockQueryReader.getQuery("sqlScript_selectUser.sql")).thenReturn("query");
Mockito.when(mockConnection.prepareStatement("query")).thenReturn(mockPreparedStatement);
Mockito.when(mockPreparedStatement.executeQuery()).thenReturn(mockResultSet);
Mockito.when(mockResultSet.next()).thenReturn(true);

Mockito.when(mockResultSet.getString(1)).thenReturn("message");
User u=userDAO.getUserById("AB1234");
assertEquals("EX112233", u.getAssociateId());
}

我的测试应该失败,因为我返回“消息”但是,我用“EX112233”断言

但它正在调用数据库而不是模拟。

提前致谢。

最佳答案

您是否准备了 UserDAO 以在 PowerMockito 中进行测试 using @PrepareForTest

请记住,您需要准备调用 new DBConnection()new QueryReader() 的类,以使这些构造函数 stub 生效。

关于java - Mockito——模拟数据库调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22649651/

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