There are two approaches. Firstly, you are mocking what is necessary for the correct interaction with Foo (as mentioned in another answer), or secondly, you extract all the complexity to separate the dependency.
In your example, complexity is creating messages. You can either:
class FooLogMessageFactory { public string createLogSomethingMessage(Foo foo) { return "foo did something." + foo.getA() + foo.getB().getC(); } }
Or the whole part of the magazine in a separate class:
class FooDedicatedLogger { public void logOnSomething(Foo foo) { String message = "foo did something." + foo.getA() + foo.getB().getC(); logger.log(logFile, message); } }
This, of course, is introduced into the Example class as a constructor dependency. Then, in the test, you can easily mock it and forget about creating the entire log message (as it should be, given that this is not related to what the actual tested method does).
Now, whichever approach is chosen to a large extent, depends on the complexity of the objects involved (i.e., how difficult is it to actually create log messages? More complicated than setting up some extra calls?).
source share