How to cover this line of code contain java Annotation in unit testing using Jmock? -


this code retrive data database , match column name data retrieve annotate field name . how cover lines of code in unit testing using jmock ? please me out of problem.

        arraylist<string> criticalfields = getallcriticalfield("ord", order                 .getsrcsysid());         if (criticalfields != null && criticalfields.size() >= 1) {             string query = "select *  tvs.ord src_sys_id='"                     + order.getsrcsysid() + "' , ord_id='"                     + order.getordid() + "'";             connection = vwsutil.getconnection();             statement = connection.createstatement();              field[] fields = order.class.getdeclaredfields();              (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 + " , "                                         + column.columnname() + " 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 + " , "                                         + column.columnname() + "='"                                         + sqldate + "'";*/                                 query = query + " , "                                         + 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 + " , "                                         + column.columnname() + " 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 + " , "                                         + column.columnname() + "='"                                         + sqldate + "'";                                 /*query = query + " , "                                         + 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 + " , "                                         + column.columnname() + " null ";                             } else {                                 query = query + " , "                                         + column.columnname() + "="                                         + field.get(order).tostring();                             }                          } else {                             if (field.get(order) == null                                     || field.get(order).tostring()                                             .isempty()) {                                  query = query + " , "                                         + column.columnname() + " null ";                             } else {                                 query = query + " , "                                         + 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;             }         } 

not sure problem having - code going hard understand , hard unit test in current form.

your life going lot easy if refactor beast distinct responsibilities.

the bulk of code looks generating sql strings - that's 1 coarse responsibility ought easy test independently once it's been pulled out.

once sql generation pulled out, testing interaction database easier.

of course, refactor safely need tests.

the current structure of code makes unit tests difficult write. before start i'd therefore suggest put lot of integration test coverage in place - i.e tests exercise code , hit database.


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -