I have ClassA and ClassB, i have some instrumentation dependencies instantiated in Class B constructor so i want to avoid Class B constructor being called when testing Class A's foo method. Class A's foo method creates an instance of class B. I am using mockito is there any way to tell test case that when TestClassA.foo() method is called don't instantiate ClassB but return dummy ClassB.
Asked
Active
Viewed 295 times
1 Answers
1
I solved this problem of mine by delegating the Class B instantiating responsibility to a Factory class.
Then while testing used mockito to mock the factory class and called the makeClassB() method to get instance of ClassB, also used Mockito.when(factoryMock.makeClassB(anyParameters)).thenReturn(MockClassB)
And now in TestClassA i can call ClassA's foo() method which now uses Factory class to create instance of ClassB instead of directly creating ClassB instance.
check this post https://stackoverflow.com/a/21262999/3805770.

KingKongCoder
- 600
- 4
- 15
-
Thanks for posting your solution! – Timothy Truckle Nov 23 '17 at 08:00