I am new in unit testing and my manger recommended Mockito. I am exploring this tool but having the some confusion like:
Why we need to create the mock object while we can create the real object of any class.
Why we set the behavior of any method using stubbing like 'when and then ' condition while developer set the behavior of the method.
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.