Skip to content Skip to sidebar Skip to footer

Unit Test Avoid Class B Constructor Call In Class A Test Method

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 fo

Solution 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.

Post a Comment for "Unit Test Avoid Class B Constructor Call In Class A Test Method"