abhi
abhi

Reputation: 41

While writing scala test cases not able to compare List of strings or Array of strings

I am parsing JSON string which is Result(List(Array(String))). When I write a test case for this it fails as instances are different:

Some([Ljava.lang.String;@486bc9a4) Some([Ljava.lang.String;@4cbf4f53)
parse(jsonString) shouldBe Result(List(Array("success")))

case class definition:

case class Message(id: Int, status: String)

case class Result(id: Int, message: List[Array[Message]])

parsing:

    def parseJson(): Result = {
        val json = parse(
         """
          {
             "id":1,
              [
               message:{
                 "id": 10001
                 "status": "success"
               },
               message:{
                 "id": 10001
                 "status": "success"
               }
              ]
            }"""
    (json.camelizeKeys).extract[Result]
   }

while testing when I call the above method it returns something like below:

Right(Result(1, Message(10001,[Ljava.lang.String;@4cbf4f53)))

when I test above method instead of getting actual string I get object instances which vary every time I execute this code. How to test this code

Upvotes: 0

Views: 452

Answers (2)

Feyyaz
Feyyaz

Reputation: 3216

Usually, it's not a good practice to use Array in a case class, because it is nothing but a Java array, which means the equals comparison will result in false even if they have the same objects inside. (Another reason is that it's mutable!).

Assuming you're using ScalaTest, shouldBe would still work for directly comparing the Array objects, because it does a deep comparison:

Array("A", "B") shouldBe Array("A", "B") // this is fine

However, for case class, shouldBe delegates the responsibility of comparison to the case class, which will result in false because the arrays almost always point to different objects.

As to your sample code, the JSON is invalid and needs to be corrected as below:

{
  "id": 1,
  "messages": [
    {"id": 10001,"status": "success"},
    {"id": 10001,"status": "success"}
  ]
}

and the case class would be

case class Result(id: Int, messages: List[Message])

Now, you're free of Arrays, and you can safely do your comparisons like below:

parse("...") shouldBe Result(1, List(...))

Upvotes: 1

pedrorijo91
pedrorijo91

Reputation: 7865

you can convert an array to list: myArray.toList

or if you have an option:

myOpt.map(_.toList)

but your question is not very clear. let me know if this is not exactly what you are asking (add more details - maybe your test code)

Upvotes: 0

Related Questions