1

The following post partly answers my question except from the fact that my class under test is using several other independent classes and I would like to mock all of them.

JUnit. Mockito. Mock class in class under test

I could inject each of the classes, but I think it's ugly. I could make a generic factory that can instantiate classes of any kind, but I don't want this factory to depend on several different classes because it seems ugly too.

Some input would be very much appreciated. Thanks.

Btw. the project is written in C++.

Salmony
  • 11
  • 1
  • Can you mock the complete header and definition file of those classes? Why exactly do you need to mock those classes in the first place? – MikeMB Aug 23 '17 at 15:05
  • @MikeMB It's an option, yes, but I'm using Google Test/Mock and those guys are advising differently. I want to mock them because I'm unit testing the class under test. It makes no sense for the design to test those classes as a part of this particular unit test. – Salmony Aug 23 '17 at 16:58
  • I'm asking because I usually only mock objects that make testing the unit under test significantly more difficult (e.g. because they need a network connection) and in those cases l try to use dependency injection anyway. Otherwise I don't bother with mocking and just make sure that those classes are properly tested before. – MikeMB Aug 23 '17 at 17:31
  • @MikeMB I see your point, but in my case one of the classes is a serial port and another involves some multithreading stuff which I would very much like not to include in my unit test. But you do make another good point making sure that those classes are properly tested - which they are not (yet). – Salmony Aug 23 '17 at 18:49
  • Well, the serial I/Of is defibrillator a candidate for DI. And if multi-threaded is part of your class, you probably also want to test it with it. But of course I don't know your code, so this is largely guesswork. Sorry that I don't have an answer to your actual question. I hope the answer isn't just "you can't". – MikeMB Aug 24 '17 at 12:10

0 Answers0