LoginSignup
15
4

More than 1 year has passed since last update.

日付のテストは午後にやろう

Last updated at Posted at 2021-09-15

SimpleDateFormatHHhh は結構な確率で間違えます。
結合テストが軌道に乗ってきた頃に、午後にならないと再現しない謎の不具合が発生します。

// 現在日時: 2021-09-15 13:24:56

DateFormat df1 = new SimpleDateFormat("yyyyMMddHHmmss");
// => 20210915 *13* 2456

DateFormat df2 = new SimpleDateFormat("yyyyMMddhhmmss");
// => 20210915 *01* 2456

面倒なので単体テストでちゃんと検知しましょう。
ということで、午後の日付を入力値にするのがポイントです。

以下はdjUnitでやるときの例。

// 現在日時を固定(超てきとう)
MockObjectManager.addReturnValue(Calendar.class, "getTime", Timestamp.valueOf("2021-09-15 13:24:56"));

// テスト実行&結果の検証
String result = testObj.test();
assertEquals("20210915132456", result);
15
4
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
4