How to mock scala obejct
See original GitHub issuehi , I tried to mock Scala with 1.16.0, but the result was an error. This is my code. What’s wrong with it? thanks my pom <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-scala_2.11</artifactId> <version>1.16.0</version> </dependency> my scala 2.11.8 my obejct
object FooObject { def simpleMethod: String = “not mocked!” }
object MockObject { def main(args: Array[String]): Unit = { withObjectMocked[FooObject.type] { FooObject.simpleMethod returns “mocked!” //or //when(FooObject.simpleMethod) thenReturn “mocked!” FooObject.simpleMethod == “mocked!” } FooObject.simpleMethod == “not mocked!” } }
error:
Exception in thread "main" org.mockito.exceptions.base.MockitoException: Cannot mock/spy class FooObject$ Mockito cannot mock/spy because : final class at MockObject$.main(MockObject.scala:10) at MockObject.main(MockObject.scala)
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:6 (2 by maintainers)
Top GitHub Comments
It seems I forgot to mention it in the docs, you need to use the inline mock maker for this to work
Sure. It’s my pleasure