Search code examples
scalatimestampscalatest

Comparing OffsetDateTime to Timestamp in ScalaTest


I'm quite new in Scala world

In my Scala tests I have java.time.OffsetDateTime and java.sql.Timestamp

offsetDateTimeValue shouldBe timestampValue

Result:

Expected :2019-06-20T16:19:57.988Z
Actual   :2019-06-20 16:19:57.988

Any ideas? I was thinking to implement a custom matcher but got stuck with that


Solution

  • I ended up implementing a custom matcher since I needed it in many tests

    def beTheSameDate(right: OffsetDateTime) = DateTestMatcher(right)
    
    case class DateTestMatcher(right: OffsetDateTime) extends Matcher[Timestamp] {
      override def apply(left: Timestamp): MatchResult = {
        val areEqual = left.toLocalDateTime == right.toLocalDateTime
        MatchResult(areEqual, "Dates are not equal", "Dates are equal")
      }
    

    And then

    timestampValue should beTheSameDate(offsetDateTimeValue)