0

I have written a Scala testcase and run it through:

sbt > test

It fails, which is fine. I have issue with the output of the diff though as it is basically just:

[String] did not match [String]

leaving it up to me to find the difference. Esp. when comparing two Seq it becomes hard to read and find the problem, e.g.:

[info]   List(CruisePrice(aid,de,Wed Feb 15 00:00:00 CET 2017,Wed Mar 01 00:00:00 CET 2017,sailId,2000,3000,true,2,0,0,0,1000,2500.0,3000.0,SOME_RATE_CODE,SOME_RATE_DESCRIPTION,EUR,Sun Jan 01 00:00:00 CET 2017), CruisePrice(aid,de,Wed Feb 15 00:00:00 CET 2017,Wed Mar 01 00:00:00 CET 2017,sailId,9001,3000,true,2,0,0,0,9000,2500.0,3000.0,SOME_RATE_CODE,SOME_RATE_DESCRIPTION,EUR,Sun Jan 01 00:00:00 CET 2017)) did not equal List(CruisePrice(aid,de,Wed Feb 15 00:00:00 CET 2017,Wed Mar 01 00:00:00 CET 2017,sailId,9001,3000,true,2,0,0,0,9000,2500.0,3000.0,SOME_RATE_CODE,SOME_RATE_DESCRIPTION,EUR,Sun Jan 01 00:00:00 CET 2017)) (OverrideTest.scala:104)

Is there a way to configure for scala test to become more human readable so that they provide a more word-diff-like experience without having to parse huge chunk of text?

Or can I use a different way to assert it? As right now, I am using FlatSpec with:

assert(seq1 == seq2)
k0pernikus
  • 60,309
  • 67
  • 216
  • 347
  • Similar https://stackoverflow.com/questions/7434762/comparing-collection-contents-with-scalatest?answertab=active#tab-top – k0pernikus Nov 13 '17 at 11:39

1 Answers1

0

You can use assertEquals(obj1, obj2) to compare two strings . assertEquals uses the equals method for comparison. There is a different assert, assertSame, which uses the == operator.

To understand why == shouldn't be used with strings you need to understand what == does: it does an identity check. That is, a == b checks to see if a and b refer to the same object. It is built into the language, and its behavior cannot be changed by different classes. The equals method, on the other hand, can be overridden by classes. While its default behavior (in the Object class) is to do an identity check using the == operator, many classes, including String, override it to instead do an "equivalence" check. In the case of String, instead of checking if a and b refer to the same object, a.equals(b) checks to see if the objects they refer to are both strings that contain exactly the same characters.

Saghe Achraf
  • 330
  • 1
  • 7