如何为 ClassNotFoundExceptions 编写适当的单元测试?

How to write a proper unit test for ClassNotFoundExceptions?

我想测试出现以下行的方法:

try (Connection connection = dataSource.getConnection()) {
        ((org.postgresql.PGConnection) connection).addDataType("geometry", Class.forName("org.postgis.PGgeometry"));
        ((org.postgresql.PGConnection) connection).addDataType("box3d", Class.forName("org.postgis.PGbox3d"));

        try (Statement statement = connection.createStatement()) {
            /*
             * 4326 is the ID of a format in which the longitude and latitude values should be
             * retreived.
             */
            String sqlQuery = "SELECT ST_Transform(way, 4326) FROM planet_osm_line WHERE (highway='footway' OR highway='steps');";
            ResultSet resultSet = statement.executeQuery(sqlQuery);

            while (resultSet.next()) {
                PGgeometry geom = (PGgeometry) resultSet.getObject(1);
                LineString line = (LineString) geom.getGeometry();
                Point[] wayPoints = line.getPoints();

                pointList.add(wayPoints);
            }
        }
    } catch (SQLException | ClassNotFoundException e) {
        throw new OpenStreetMapDAOException(e.getMessage(), e);
    }

这些行迫使我赶上 ClassNotFoundException,即 Class.forName("name") 的调用确实如此。

在我的测试中从未达到 ClassNotFoundExceptioncatch 情况,因为这些 类 始终存在。 有没有办法测试我的 catch 块?

由于 org.postgresql.PGConnection 似乎是一个接口,您可以尝试通过 Mockito 或类似的模拟框架来模拟它。

org.postgresql.PGConnection connection = Mockito.mock(org.postgresql.PGConnection.class)
Mockito.doThrow( ...your exception here...).when( connection ).addDataType("geometry", Class.forName("org.postgis.PGgeometry"));

通过这两行,您可以为您的连接创建一个模拟对象,然后您可以在您的方法中使用它。当使用这些参数调用该方法时,此模拟对象将抛出给定的异常。

我根据我的问题重构了代码,直到我得到以下关键部分的方法,即那些 Class.forName("some.class.name") 调用:

Statement createStatement() throws SQLException, ClassNotFoundException {
    Connection connection = dataSource.getConnection();

    ((org.postgresql.PGConnection) connection).addDataType("geometry", Class.forName("org.postgis.PGgeometry"));
    ((org.postgresql.PGConnection) connection).addDataType("box3d", Class.forName("org.postgis.PGbox3d"));

    return connection.createStatement();
}

然后在我的单元测试中我使用了

when(dao.createStatement()).thenThrow(ClassNotFoundException.class);

终于解决了我的问题。