question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to mock scala obejct

See original GitHub issue

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 <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:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
bbonannocommented, 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

0reactions
rhh777commented, 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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mocking scala object - Stack Overflow
I am using mockito and trying to mock a scala object. object Sample { } //test class SomeTest extends Specification with ScalaTest with ......
Read more >
Testing With Mock Objects - ScalaTest
ScalaMock is a native, open-source Scala mocking framework written by Paul Butcher that allows you to mock objects and functions. ScalaMock supports three ......
Read more >
How to use Mock objects with ScalaTest | alvinalexander.com
There are many more ways to use Mockito and other mock object frameworks in your ScalaTest tests. These links will help you get...
Read more >
Beginners Guide to Mocking in Scala - DZone
That is when mocking comes into the picture. The idea behind mocking is that we will create a mock object so that it...
Read more >
User Guide - Choosing your mocking style - ScalaMock
ScalaMock supports two different mocking styles—expectations-first and record-then-verify. These styles can be mixed within a single test. Expectations-First ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found