在Hibernate中对回调方法进行单元测试

时间:2017-11-12 16:33:15

标签: java hibernate unit-testing

有人可以向我解释是否需要为Hibernate回调方法编写单元测试?我的意思是带有注释@PrePresist,@ PreUpdate等的实体方法。

这是我的Entity类的一部分,包含所需的字段。我的意思是字段" startDate"和" endDate"。正如您所看到的,prePersistStartDate和prePersistEndDate方法中存在某些逻辑,用于确定将设置哪些值。所以我的问题是有必要为这些方法创建单元测试。

@Entity
@Table(name = "Budget")
public class Budget implements Serializable, Comparable<Budget> {
  // *** Fields *** //

  @Transient
  private Calendar initCalendar = Calendar.getInstance();

  @Id
  @GeneratedValue
  @Column(name = "id", nullable = false)
  private Integer id;

  ...
  ...
  ...

  @Enumerated(EnumType.ORDINAL)
  @Column(name = "range", length = 15, nullable = false)
  private Range range;

  @Column(name = "by_date_range")
  private Boolean byDateRange;

  @Temporal(TemporalType.DATE)
  @Column(name = "start_date", nullable = false)
  private Date startDate;

  /**
   * Set up value for start date.
   */
  @PrePersist
  @PreUpdate
  public void prePersistStartDate() {
    if (!byDateRange) {
      Calendar calendar = Calendar.getInstance();
      calendar.setTimeInMillis(initCalendar.getTimeInMillis());

      switch (range) {
        case CURRENT_WEEK:
          // set Monday as the start day of week (value = 2)
          calendar.add(Calendar.DAY_OF_WEEK, 2 - calendar.get(Calendar.DAY_OF_WEEK));
          break;

        case LAST_WEEK:
          // subtract one week
          calendar.add(Calendar.WEEK_OF_MONTH, -1);
          // set Monday as the start day of week (value = 2)
          calendar.add(Calendar.DAY_OF_WEEK, 2 - calendar.get(Calendar.DAY_OF_WEEK));
          break;

        case CURRENT_MONTH:
          // set first day of the current Month
          calendar.set(Calendar.DAY_OF_MONTH, 1);
          break;

        case LAST_MONTH:
          // subtract one month
          calendar.add(Calendar.MONTH, -1);
          // set first day of the last Month
          calendar.set(Calendar.DAY_OF_MONTH, 1);
          break;

        case CURRENT_YEAR:
          // set first day of the current Year
          calendar.set(Calendar.DAY_OF_YEAR, 1);
          break;

        case LAST_YEAR:
          // subtract one year
          calendar.add(Calendar.YEAR, -1);
          // set first day of the last Year
          calendar.set(Calendar.DAY_OF_YEAR, 1);
          break;

        default:
      }

      startDate = calendar.getTime();
    }
  }

  @Temporal(TemporalType.DATE)
  @Column(name = "end_date", nullable = false)
  private Date endDate;

  /**
   * Set up value for end date.
   */
  @PrePersist
  @PreUpdate
  public void prePersistEndDate() {
    if (!byDateRange) {
      Calendar calendar = Calendar.getInstance();
      calendar.setTimeInMillis(initCalendar.getTimeInMillis());

      switch (range) {
        case CURRENT_WEEK:
          // set the last day of the current week
          calendar.add(Calendar.DAY_OF_WEEK, 7 - calendar.get(Calendar.DAY_OF_WEEK));
          break;

        case LAST_WEEK:
          // subtract one week
          calendar.add(Calendar.WEEK_OF_MONTH, -1);
          // set the last day of the current week
          calendar.add(Calendar.DAY_OF_WEEK, 7 - calendar.get(Calendar.DAY_OF_WEEK));
          break;

        case CURRENT_MONTH:
          // set last day of the current Month
          calendar.set(Calendar.MONTH, calendar.getActualMaximum(Calendar.MONTH));
          break;

        case LAST_MONTH:
          // subtract one month
          calendar.add(Calendar.MONTH, -1);
          // set last day of the last Month
          calendar.set(Calendar.MONTH, calendar.getActualMaximum(Calendar.MONTH));
          break;

        case CURRENT_YEAR:
          // set last day of the current Year
          calendar.set(Calendar.DAY_OF_YEAR, calendar.getActualMaximum(Calendar.YEAR));
          break;

        case LAST_YEAR:
          // subtract one year
          calendar.add(Calendar.YEAR, -1);
          // set last day of the last Year
          calendar.set(Calendar.DAY_OF_YEAR, calendar.getActualMaximum(Calendar.YEAR));
          break;

        default:
      }

      endDate = calendar.getTime();
    }
  }

  public enum Range {
    CURRENT_WEEK, LAST_WEEK, CURRENT_MONTH, LAST_MONTH, CURRENT_YEAR, LAST_YEAR
  }
}

0 个答案:

没有答案