4

I need to globally mock class method.

I mean, i can't create mocked object and stub methods. My api doesn't take this object as an argument so i cant pass it inside function calls, but object of this class are created inside those functions and used there. That's why I need to mock it globally.

class A {
  public void methodA() {}  
}

I need to mock methodA().

I can imagine it might be nearly impossible to achive by mockito. Does anyone have idea how to achive this ? Or should i rewrite my code ?

Kasper Ziemianek
  • 1,329
  • 8
  • 15
  • You're unclear. What does "global" mean? What exactly do you need to do? You create a mocked instance of a class with the "mock()" call, and you constrain behavior of methods with variations of "when()" on that mocked instance. I assume you know all of this, so what exactly is your problem? Perhaps you should show more code? – David M. Karr Jul 04 '14 at 16:47
  • 1
    You should refactor your code so that it doesn't create new objects deep within methods. There are [some ideas here](https://code.google.com/p/mockito/wiki/MockingObjectCreation) on how to do this. – Dawood ibn Kareem Jul 04 '14 at 18:18
  • The better is a refactor, you could for example introduce a `factory`. Or if you can't or it is to expensive, try with powermock. – gontard Jul 04 '14 at 18:25
  • @DavidM.Karr "global" mean, mock this method for every instance of this class, not only one created with mock() mockito static method. I think I should avoid creating objects deep inside methods, this might solve my issue, thanks for idea. – Kasper Ziemianek Jul 04 '14 at 19:30
  • Google's documentation on MockingObjectCreation was enlightening. Thanks @Dawood ibn Kareem – whitehat Aug 24 '17 at 22:35
  • @whitehat - No problem. By the way, it's not Google's documentation. It belongs to the Mockito team. Google just host it for us. I am the principal author of that document, although I think some other members of the Mockito team have tweaked it a bit since I first wrote it. – Dawood ibn Kareem Aug 25 '17 at 04:35

2 Answers2

0

Also in a unit test, just stub the call in a @Before method. NBut you may need to refactor your code. A factory would seem wise, and you could inject that factory and stub it for the whole test class using the same trick below.

@Before public void stub_sessionService_userId() {
  given(sessionService_mock.userId()).willReturn(73L);
}
bric3
  • 40,072
  • 9
  • 91
  • 111
0

You should rewrite your code.

You wrote:

object of this class are created inside those functions and used there

This is something which is generally a bad idea if you want to have a testable system. The functions you mention should not create the object themselves, ideed they should not even know where they come from or who is providing that object.

A better approach is by having those functions work with interfaces. Implementors for that interface will then be arguments to the function or be injected to the object to which that function belongs , for example via a contrcutor.

Read up about Inversion of control and Dependency Injection for those matters. These concepts are crucial to maintain a modular and thus testable system. Many frameworks for application development provide the necessary tools to develop applications using dependency injection.

benjamin
  • 1,066
  • 11
  • 22