Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mock a class with scala mock

Tags:

scalamock

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 constructor.

Many thanks,

Maatari

like image 574
MaatDeamon Avatar asked Jun 14 '15 23:06

MaatDeamon


1 Answers

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]
like image 200
Pawel Wiejacha Avatar answered Dec 11 '22 11:12

Pawel Wiejacha