0

I've Class A that has an instance of another Class B. I've completed unit testing of independent code in Class B. For other operations, Class A invokes methods of Class B, does some processing using other classes and then invokes methods of Class B.

My question is how do I mock the dependency of Class A and complete unit testing of Class B? If Class B uses instance of Class A, I can mock Class A and complete the task. However, in my case Class A uses instance of Class B.

How do I mock in this scenario for unit testing?

JSN
  • 2,035
  • 13
  • 27

1 Answers1

0

This depends on how you have designed your class A and class B.

If they have been well-designed then you can easily make mock implementation for interface IB - interface of B - (using GoogleTest & GoogleMock) then use this mock implementation in unit tests of class A. To see how to use GoogleMock, check GoogleMock CheatSheet.

Otherwise, you may have run into a situation similar to Google Mock: Mock private variable member that is instantiated in target class's constructor then you may need to use some preprocessor directives to break your class A.

duong_dajgja
  • 4,196
  • 1
  • 38
  • 65