Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocking scala object

I am using mockito and trying to mock a scala object.

object Sample { }
//test
class SomeTest extends Specification with ScalaTest with Mockito {
    "mocking should succeed" in {
        val mockedSample = mock[Sample]
     }
}

This gives me two compilation errors.

error: Not found type Sample
error: could not find implicit value for parameter m:
scala.reflect.ClassManifest[<error>]

If I change Sample from object to class it works. Is is possible to mock scala objects with mockito? If yes how?

like image 936
scout Avatar asked Aug 26 '10 14:08

scout


People also ask

Can I mock a Scala object?

You can use any Java mocking framework with ScalaTest, or ScalaMock, a Scala mocking alternative. ScalaTest provides just enough syntax sugar for the three most popular Java mocking frameworks—JMock, EasyMock, and Mockito—to remove boilerplate and clarify the client code.

What is mocking in Scala?

Mocking is a process used in software testing to test code that depends on external resources. This helps us to independently test the core logic without worrying about the external APIs. Mockito is one of the most popular mocking frameworks in the JVM.


1 Answers

As written, your Sample is a pure singleton. Its type is its own and there is only one member of that type, period. Scala objects can extend another class (possibly abstract, if it supplies the necessary definitions to make it a concrete) and traits. Doing that gives it a type identity that includes those ancestors.

I don't know what Mockito is really doing, but to my mind, what you're asking for is strictly at odds with what a Scala object is.

like image 177
Randall Schulz Avatar answered Sep 19 '22 11:09

Randall Schulz