How to mock a class with ScalaMock

11.3k views Asked by At

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.

1

There are 1 answers

2
Pawel Wiejacha On BEST ANSWER

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]