Partial EMMA code coverage in Scala Case Class for IntelliJ IDEA 10.5
Asked Answered
P

2

7

I'm using IntelliJ IDEA 10.5 with the Scala plugin v0.4.1338 updated on August 14th, and Scala 2.9.0.1. I recently began using the EMMA test coverage utility in IDEA to generate coverage reports.

I cannot determine why the constructor line of my Scala case class is only showing partial (yellow) coverage. I have looked in the EMMA FAQs and researched the matter online with no success. Does anyone have any idea how I can reach 100% coverage on a case class?

Phylissphyll answered 16/8, 2011 at 20:6 Comment(0)
Z
4

case class A(a: Any) generate a number of methods for you, among them:

  1. A#equals
  2. A#canEqual
  3. A#hashCode
  4. A#toString
  5. A#productPrefix
  6. A#productElement
  7. A#productArity
  8. A#productIterator
  9. A#copy
  10. A.unapply
  11. A.apply

Most of these will be reported in the bytecode at the same line number as the class definition.

You could write a reflective utility to call all of these methods in each unit test for your case classes, patch the code coverage tool to ignore that line, or just put up with it.

Zipporah answered 16/8, 2011 at 21:47 Comment(2)
Yes, this seems to be correct. On a hunch I wrote a few tests for the implicit hashCode() and equals() function, and the reported line coverage did increase. Thank you!Phylissphyll
Care to share a gist that automates this? This would be one heck of an awesome trait to slap on WordSpecs or FunSpecs to take care of case classes auto magically.Improvident
E
5

I know this is a very old question, but the problem still stands today to some extent. Given a simple case class, in order to get a full coverage report from IntelliJ you need to test the unapply method as well.

// Code

final case class Foo(symbol: String, name: String)

// Test

val myFoo = Foo("TheSymbol", "TheName")

Foo.unapply(myFoo).get should be(("TheSymbol", "TheName"))

Without it I got 50% coverage for a basic case class like that.

Effectual answered 20/4, 2019 at 3:15 Comment(0)
Z
4

case class A(a: Any) generate a number of methods for you, among them:

  1. A#equals
  2. A#canEqual
  3. A#hashCode
  4. A#toString
  5. A#productPrefix
  6. A#productElement
  7. A#productArity
  8. A#productIterator
  9. A#copy
  10. A.unapply
  11. A.apply

Most of these will be reported in the bytecode at the same line number as the class definition.

You could write a reflective utility to call all of these methods in each unit test for your case classes, patch the code coverage tool to ignore that line, or just put up with it.

Zipporah answered 16/8, 2011 at 21:47 Comment(2)
Yes, this seems to be correct. On a hunch I wrote a few tests for the implicit hashCode() and equals() function, and the reported line coverage did increase. Thank you!Phylissphyll
Care to share a gist that automates this? This would be one heck of an awesome trait to slap on WordSpecs or FunSpecs to take care of case classes auto magically.Improvident

© 2022 - 2024 — McMap. All rights reserved.