samedi 2 juillet 2016

JUnit - SQLException, how to get coverage?

I know I can use Mockito to force an exception throw from DAO methods, but how can I get test coverage inside the DAO class properly?

Example:

public static void addUser(int userId, int groupId)
        throws DataSourceException {
    String query = "INSERT INTO RUserGroup (userId, groupId) VALUES (?, ?)";
    Connection connection = Pool.getInstance().getConnection();
    try (PreparedStatement ps = connection.prepareStatement(query);) {
    // ...
    } catch (SQLException e) {
        throw new DataSourceException(e);
    } finally {
        Pool.getInstance().returnConnection(connection);
    }
}

How can I reach the "throw new DataSourceException(e)"? I have a lot of those cases in the code and they completely ruin branch coverage. I am using JaCoCo for the coverage.

Aucun commentaire:

Enregistrer un commentaire