Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to mock scala obejct #303

Closed
rhh777 opened this issue Oct 21, 2020 · 6 comments
Closed

How to mock scala obejct #303

rhh777 opened this issue Oct 21, 2020 · 6 comments

Comments

@rhh777
Copy link

rhh777 commented Oct 21, 2020

hi , 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

org.mockito
mockito-scala_2.11
1.16.0

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)

@ultrasecreth
Copy link
Member

It seems I forgot to mention it in the docs, you need to use the inline mock maker for this to work

@rhh777
Copy link
Author

rhh777 commented Oct 21, 2020

It seems I forgot to mention it in the docs, you need to use the inline mock maker for this to work

Sorry, I have checked the test package, I still don't quite understand what inline Mock Maker is, can you provide an example? thanks

@LanceEa
Copy link

LanceEa commented Oct 21, 2020

@QuietHR - I was able to get it to work once I added MockMaker plugin per the release notes on Mockito - Mock Maker

Screen Shot 2020-10-21 at 9 27 37 AM

@rhh777
Copy link
Author

rhh777 commented Oct 22, 2020

@QuietHR - I was able to get it to work once I added MockMaker plugin per the release notes on Mockito - Mock Maker

Screen Shot 2020-10-21 at 9 27 37 AM

图片

Thank you very much. According to your method, the mock succeeded ~

@ultrasecreth
Copy link
Member

Would you mind guys raising a PR to add those instructions on the README? I'm super busy these days but I can easily merge a PR :)

@rhh777
Copy link
Author

rhh777 commented Oct 22, 2020

Would you mind guys raising a PR to add those instructions on the README? I'm super busy these days but I can easily merge a PR :)

Sure. It's my pleasure

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants