G
G
goshan_p2016-03-08 14:12:56
MySQL
goshan_p, 2016-03-08 14:12:56

How to test DAO layer in Java with Mockito?

Good day. Could you give an example of testing a DAO layer using mock objects? Or advise a resource where you can read about it.
If you point out errors in the code, I will only be grateful)
DataSource

public class DataSource {

    public static final Logger log = Logger.getLogger(DataSource.class);

    BasicDataSource connectionPool;

    public DataSource() {

        Properties prop = getProperties();

        log.info("Connection pool init...");

        connectionPool = new BasicDataSource();
        connectionPool.setUsername(prop.getProperty("db.username"));
        connectionPool.setPassword(prop.getProperty("db.password"));
        connectionPool.setDriverClassName("db.driver");
        connectionPool.setUrl("db.url");
        connectionPool.setInitialSize(1);

        log.info("Connection pool ready");
    }

    private Properties getProperties(){

        Properties properties = new Properties();

        try {
            InputStream in = getClass().getClassLoader().getResourceAsStream("db.properties");
            properties.load(in);
            return properties;
        } catch (IOException e) {
            log.warn("Error of reading db.properties.");
        }
        return properties;
    }

    public Connection getConnection() throws SQLException {
        Connection connection = connectionPool.getConnection();
        connection.setAutoCommit(false);
        return connection;
    }
}

Method to test
public class UserDaoImpl implements UserDao {

    public UserDaoImpl() {
        this.dataSource = new DataSource();
    }

    public User findByEmail(String email) {

        log.info("Search by email: " + email);

        User user = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            Connection connection = dataSource.getConnection();
            preparedStatement = connection.prepareStatement(FIND_BY_ID);
            preparedStatement.setString(1,email);
            resultSet = preparedStatement.executeQuery();
            user = new User(resultSet);

            return user;
        } catch (SQLException e) {
            log.warn("SQLException in findByEmail()");
        } finally {

            if(preparedStatement != null){

                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                   log.warn("Prepared statement not closed");
                }

            }
        }
        return user;
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2016-03-08
Protko @Fesor

They mock the DAO itself, no one in their right mind mocks the Connection/DataSource, etc. since it's too expensive to maintain such tests, it's easier to write a simple integration test that uses a real database.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question