사용자 세부 정보를 반환하는 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"
로 주장하고 "메시지"를 반환하고 같이 내 테스트가 실패한다하지만 조롱하는 대신 데이터베이스를 호출하고 있습니다.
미리 감사드립니다.
DAO를 테스트하기 위해 메모리 내장 DB를 사용해 보셨습니까? 이 테스트는 mock에 너무 많이 오버로드되어 지원하기가 쉽지 않습니다 .. –