0

I am new in unit testing and my manger recommended Mockito. I am exploring this tool but having the some confusion like:

  1. Why we need to create the mock object while we can create the real object of any class.

  2. Why we set the behavior of any method using stubbing like 'when and then ' condition while developer set the behavior of the method.

  3. If we do stubbing of any method then it will be static, in case developer change the code of that method then its impact will not be appeared in our test case, it will got always pass.

For example:

   TestingServices test = Mockito.mock(TestingServices.class);

   // define return value for method getUniqueId()

    System.out.println("unique id is "+test.getUniqueId());

    when(test.getUniqueId()).thenReturn(44); 

    assertEquals(test.getUniqueId(),44); 

In above code snippet assertEquals will always pass, whether a developer changes the code or not.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Mohd Abad
  • 45
  • 1
  • 8
  • 1
    Your test is a bad example, because it's unit testing the same thing it's mocking. In a real example you would unit test A by mocking B, so that you isolate A's behaviour, and vice versa. Then you'd have higher level integration tests that check that A and B still interact correctly when neither is mocked. – jonrsharpe May 13 '16 at 06:29
  • Hi Jonrsharpe, Thanks for give me ans..actually my concern is why we need to craete mock object and stubbing?? please explain this your help would clear my doubts. – Mohd Abad May 13 '16 at 06:35
  • Hi Exoddus, can you give me more example when we create the mock object or when not needed to create it. – Mohd Abad May 13 '16 at 06:40
  • @MohdAbad you need to create mocks/stubs when you *don't* want to test the interactions, when you want to test a single unit of your application *in isolation*. – jonrsharpe May 13 '16 at 08:48

1 Answers1

0

First thing is take care of what are you testing in any case.

In your code snippet seems you are testing the class TestingServices and you shouldn't mock this class because it's behaivour is what your are testing, not how Mockito works injecting your 'fake' methods responses. Actually, in your example you are testing that Mockito inject the behaviour you want, and works as expected, nothing more.

For example: your TestingServices class has an object inside that consumes a third party API and get a response (or a DAO object used by a Controller in MVC architecture). If you are unit testing your TestingServicesor your Controller, you don't want to fail because a dependecy, and you can mock that part of the system.

Mocking let you focus on the class under test rather than the context or other classes needed in the process your are testing, and abstract the rest of components.

There are lots of questions talking about this:

Community
  • 1
  • 1
exoddus
  • 2,230
  • 17
  • 27
  • so when we need to create the mock object please give me some examples. – Mohd Abad May 13 '16 at 06:53
  • 1
    Mohd, I'm answering to your question with the code you provided . If need concrete examples about mocking, sure you'll find plenty of examples searching for ' java mock examples' for sure :) – exoddus May 13 '16 at 07:43