I have several Dao classes, including a UserDao, below. The DAOs have many methods, but I'm focussing on deleteUser
:
@Override
public boolean deleteUser(Connection connection,String login) throws MySQLEXContainer.MySQLDBExecutionException, SQLException {
int rowNum = 0;
Connection con;
PreparedStatement statement = null;
try {
String query = QueriesUtil.getQuery("deleteUser");
con = connection;
statement = con.prepareStatement(query);
statement.setString(1, login);
rowNum = statement.executeUpdate();
} catch (SQLException e) {
LOGGER.error(e);
throw new MySQLEXContainer.MySQLDBExecutionException("Bad execution",e);
}finally {
ConnectionUtil.oneMethodToCloseThemAll(null ,statement,null);
}
return rowNum > 0;
}
Test class for that Dao:
class MySQLUserDaoTest {
private Connection connection;
private PreparedStatement preparedStatement;
private UserDao userDao;
private ResultSet resultSet;
@BeforeEach
void init() throws SQLException {
userDao = new MySQLUserDao();
preparedStatement = mock(PreparedStatement.class);
connection = mock(Connection.class);
resultSet = mock(ResultSet.class);
when(connection.prepareStatement(anyString())).thenReturn(preparedStatement);
}
@Test
void deleteUser() throws SQLException, MySQLEXContainer.MySQLDBExecutionException {
String login = "Login";
when(connection.prepareStatement(anyString()).executeUpdate()).thenReturn(1);
boolean result = userDao.deleteUser(connection,login);
assertTrue(result);
}
}
Am I using Mockito effectively or could my tests be improved?