6

In the doc of ScalaMock, it is said that:

mocking classes, traits and case classes

is one of the feature supported.

I have the following case class:

case class Thing(private val item: Item)

When I do:

val item = mock[Thing]

I get the following error:

Error:(18, 24) not enough arguments for constructor Thing:
 (item: org.dspace.content.Item)org.iadb.poolpartyconnector.dspaceutils.Thing.
Unspecified value parameter item.
    val item = mock[Thing]
                   ^

I know I could implement an interface for it, but in any case, this would help me better understand how to mock a case class/a class that has a constructor.

halfer
  • 19,824
  • 17
  • 99
  • 186
MaatDeamon
  • 9,532
  • 9
  • 60
  • 127
  • Related: https://stackoverflow.com/questions/14274020/scalamock-mock-a-class-that-takes-arguments – dskrvk Feb 12 '16 at 17:56

1 Answers1

16

Currently, you cannot mock classes that do not have a default constructor defined.

You can workaround it by creating a subclass that has default constructor defined and mocking that subclass:

class MockableThing extends Thing(null)
val item = mock[MockableThing]
Neil
  • 24,551
  • 15
  • 60
  • 81
Pawel Wiejacha
  • 704
  • 7
  • 5