【问题标题】:Hamcrest Date MatchersHamcrest 日期匹配器
【发布时间】:2011-12-23 20:05:28
【问题描述】:

我需要在某个测试用例的日期之前/之后进行测试。如果可能,我想使用Hamcrest matchers

Hamcrest (Java) 是否有匹配器用于处理日期?如果是这样,我会在哪个包/类中找到特定的日期匹配器功能?

【问题讨论】:

    标签: java date hamcrest


    【解决方案1】:

    OrderingComparison::greaterThan 匹配器适用于与其自身相当的任何类型(它在 org.hamcrest.number 包中,但实际上并不是特定于数字的)。日期就是这样的类型。

    【讨论】:

    • 谢谢。似乎他们已经摆脱了类,转而使用静态工厂方法,这使得真正稳定的链接变得不可能,但我已经尽可能地修复它。
    • 确实如此。还有一些扩展提供了一些更易于阅读的方法。例如,Cirneco 提供匹配器 J7Matchers::after,它是 OrderingComparison::greaterThan 的别名。从我的角度来看,语义在单元测试中总是很重要的,这就是为什么我通常更喜欢 Google Truth 提供的 fulent 方法,但我有时不得不在遗留项目中处理 Hamcrest。
    【解决方案2】:

    https://github.com/eXparity/hamcrest-date 的库提供了一个 hamcrest 日期匹配器库,它也可用于 maven、ivy 等,位于

    <dependency>
        <groupId>org.exparity</groupId>
        <artifactId>hamcrest-date</artifactId>
        <version>1.1.0</version>
    </dependency>
    

    它支持各种日期匹配器,因此允许构造诸如

    Date myBirthday = new Date();
    MatcherAssert.assertThat(myBirthday, DateMatchers.after(Moments.today()));
    

    Date myBirthday = new Date();
    MatcherAssert.assertThat(myBirthday, DateMatchers.isToday());
    

    【讨论】:

      【解决方案3】:

      你可以看看将添加到 hamcrest 的新日期匹配器(我不知道什么时候想到的):

      Date matchers discussion/code changes on github

      快速浏览后似乎会有一个新包 org.hamcrest.date 包含:

      • 之后
      • 之前是
      • IsSameDayOfTheMonth
      • IsSameDayOfTheWeek
      • IsSameDayOfTheYear
      • IsSameHour
      • IsSameInstant
      • IsSameMinute
      • IsSameMonth
      • IsSameSecond
      • 同年
      • 在里面

      【讨论】:

        【解决方案4】:

        有一些 hamcrest 扩展可以简化一些与日期相关的测试。请check here.

        【讨论】:

          【解决方案5】:

          Matchers#greaterThan 匹配器适用于 Dates 和其他 Comparable 对象。

          这是检查您的日期是否大于或等于 (≥) 某个预期日期的方法:

          import static org.hamcrest.Matchers.equalTo;
          import static org.hamcrest.Matchers.greaterThan;
          import static org.hamcrest.core.AnyOf.anyOf;
          ...
          
          Date expectedMin = new Date()
          // Execute the method being tested
          Date resultDate = getDate();
          // Validate
          assertThat(resultDate, anyOf(greaterThan(expectedMin), equalTo(expectedMin)))
          

          【讨论】:

            【解决方案6】:

            还有Cirneco extension。它有几个Date 特定匹配器(例如monday())和其他适用于日期的匹配器,因为Comparable 的实现(参见例如between()betweenInclusive())。计划还支持 JDK7 版本库中的 Joda Time 和 JDK8 版本中新的基于日期的类(主要是LocalDate)。

            你可以做这样的断言:

            final Date date = new Date();
            assertThat(date, is(monday())); // JUnit style
            given(date).assertIs(monday()); // Cirneco style
            

            您可以将以下依赖项用于符合 JDK7 的项目:

            <dependency>
              <groupId>it.ozimov</groupId>
              <artifactId>java7-hamcrest-matchers</artifactId>
              <version>0.7.0</version>
            </dependency>
            

            如果您使用的是 JDK8,则如下

            <dependency>
              <groupId>it.ozimov</groupId>
              <artifactId>java8-hamcrest-matchers</artifactId>
              <version>0.7.0</version>
            </dependency>
            

            【讨论】:

              【解决方案7】:

              https://assertj.github.io/doc/#assertj-core-recursive-comparison

              org.assertj:assertj-core:3.12.2

              assertThat(actual)
                  .usingRecursiveComparison()
                  .ignoringFieldsMatchingRegexes("fieldToIgore")
                  .isEqualTo(expected);
              

              【讨论】:

              • 这与主题有什么关系?
              猜你喜欢
              • 1970-01-01
              • 2018-10-07
              • 2015-09-15
              • 2023-04-04
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多