0
votes

I have a case class case class FOO. And would like to test a method myMethod that returns FOO() given parameter value value1.

I have a test like : myMethod(value1) should equal FOO.

The test failed with FOO did not equal FOO().

What is the difference between FOO and FOO() ?

1

1 Answers

6
votes

A case class Foo() desugars into something equivalent to

class Foo() extends Product with Serializable {
  // some methods
}

object Foo extends (() => Foo) with Serializable {
  // some methods
}

So Foo() creates an instance of the Foo class while Foo is a reference to the Foo companion object.

However if your case class Foo has no parameters you should consider making it a case object instead. If you are not abusing your case class with internal mutable state[1] all Foo() instances should be indistinguishable anyway.

[1] If you are, consider using a regular class instead.