0

此代码将从数据库中检索数据,并将从中检索数据的列名称与注释字段名称相匹配。如何使用 jmock 在单元测试中覆盖这行代码?请帮我解决这个问题。

        ArrayList<String> criticalFields = getAllCriticalField("ORD", order
                .getSrcSysId());
        if (criticalFields != null && criticalFields.size() >= 1) {
            String query = "select * from  tvs.ORD where SRC_SYS_ID='"
                    + order.getSrcSysId() + "' AND ORD_ID='"
                    + order.getOrdId() + "'";
            connection = vwsUtil.getConnection();
            statement = connection.createStatement();

            Field[] fields = Order.class.getDeclaredFields();

            for (Field field : fields) {
                field.setAccessible(true);
                if (field.isAnnotationPresent(TableColumnAnnotation.class)) {
                    TableColumnAnnotation column = field
                            .getAnnotation(TableColumnAnnotation.class);
                    if (criticalFields.contains(column.columnName())) { 
                        if (column.columnName().equalsIgnoreCase(
                                "EST_RTS_DT")
                                || column.columnName().equalsIgnoreCase(
                                        "ACT_RTS_DT")
                                || column.columnName().equalsIgnoreCase(
                                        "CMIT_SHP_DT")
                                || column.columnName().equalsIgnoreCase(
                                        "ACT_FST_SHP_DT")
                                || column.columnName().equalsIgnoreCase(
                                        "ACT_FNL_SHP_DT")
                                || column.columnName().equalsIgnoreCase(
                                        "ORD_CANC_DT")
                                || column.columnName().equalsIgnoreCase(
                                        "SHP_BEF_DT")
                                || column.columnName().equalsIgnoreCase(
                                        "SHP_AFT_DT")) {

                            if (field.get(order) == null
                                    || field.get(order).toString()
                                            .isEmpty()) {
                                query = query + " AND "
                                        + column.columnName() + " IS NULL ";
                            } else {
                                Calendar cal = (Calendar) field.get(order);
                                java.util.Date dt = cal.getTime();
                                SimpleDateFormat fmt = new SimpleDateFormat(
                                        "dd-MM-yy");
                                String sqlDate = fmt.format(dt);
                                /*query = query + " AND "
                                        + column.columnName() + "='"
                                        + sqlDate + "'";*/
                                query = query + " AND "
                                        + column.columnName() + "=to_date('"
                                        + sqlDate + "','DD-MM-YY')";

                            }
                        } else if (column.columnName().equalsIgnoreCase(
                                "ORD_TS")
                                || column.columnName().equalsIgnoreCase(
                                        "ACK_TS")) {

                            if (field.get(order) == null
                                    || field.get(order).toString()
                                            .isEmpty()) {
                                query = query + " AND "
                                        + column.columnName() + " IS NULL ";
                            } else {
                                Calendar cal = (Calendar) field.get(order);
                                java.util.Date dt = cal.getTime();
                                SimpleDateFormat fmt = new SimpleDateFormat(
                                        "dd-MM-yy HH:mm:ss.SSSSSSSSS");
                                String sqlDate = fmt.format(dt);
                                query = query + " AND "
                                        + column.columnName() + "='"
                                        + sqlDate + "'";
                                /*query = query + " AND "
                                        + column.columnName() + "=to_date('"
                                        + sqlDate + "','DD-MM-YY HH:mm:ss.SSSSSSSSS')";*/

                            }
                        } else if (column.columnName().equalsIgnoreCase(
                                "DLR_MNG_TRNSP_IND")) {
                            if (field.get(order) == null
                                    || field.get(order).toString()
                                            .isEmpty()) {
                                query = query + " AND "
                                        + column.columnName() + " IS NULL ";
                            } else {
                                query = query + " AND "
                                        + column.columnName() + "="
                                        + field.get(order).toString();
                            }

                        } else {
                            if (field.get(order) == null
                                    || field.get(order).toString()
                                            .isEmpty()) {

                                query = query + " AND "
                                        + column.columnName() + " IS NULL ";
                            } else {
                                query = query + " AND "
                                        + column.columnName() + "='"
                                        + field.get(order).toString() + "'";

                            }
                        }
                    }
                }
            } //System.out.println("Order Query  =  "+query);
            resultSet = statement.executeQuery(query);

            if (!resultSet.next()) {System.out.println("Flag true  in Order   ");
                criticalFieldFlag = true;
            }
        }
4

1 回答 1

1

不确定您具体遇到了什么问题 - 但此代码将难以理解并且难以以当前形式进行单元测试。

如果你将这头野兽重构为不同的职责,你的生活将会轻松很多。

大部分代码看起来都在生成 sql 字符串 - 这是一项粗略的职责,一旦被拉出应该很容易独立测试。

一旦 sql 生成被拉出来,测试它与数据库的交互会容易得多。

当然,要安全地重构,您需要测试。

当前的代码结构使得单元测试难以编写。因此,在您开始之前,我建议您将大量集成测试覆盖率放在适当的位置——即执行此代码并访问数据库的测试。

于 2013-08-07T14:40:28.740 回答