1
object Abc {
    method1(param1,param2):Future[Option[String]] = {
        //some work
    }
}

We are using method1 somewhere and need to stub method1 how can we do this in scala using mockito

I am doing like this :

when(method1(mockParam1, mockParam2)).thenReturn(SomeExpectedStuff)

but facing NPE .

Kai-Sheng Yang
  • 1,535
  • 4
  • 15
  • 21
cipher
  • 11
  • 2
  • Usually yes. You have to mock an Object first and the you can Mock the MethodCalls on this mocked object. Can you show your Code and your Stacktrace so that we might have a look whats not working correctly? – GJohannes Apr 17 '22 at 21:34
  • we have to use this method in some other class basically and I am not able to get the method if I mock the object thanks for answering could you explain if we can mock an object? – cipher Apr 18 '22 at 05:26
  • Objects are singletons. they cannot be tested – cipher Apr 18 '22 at 07:28
  • It does not mapper if an object is a singleton or not. You can still mock it. This is literally the core function of Mockito. If you are not showing your actual code i fear we wont get any farther. A simple example of how Mockito works in general would be https://stackoverflow.com/questions/17743141/mock-class-in-class-under-test – GJohannes Apr 18 '22 at 08:51
  • Use this link for solution [Is there any way to mock Singleton object in Scala](https://stackoverflow.com/questions/37947271/is-there-any-way-to-mock-singleton-object-in-scala) – cipher Apr 18 '22 at 10:52

0 Answers0