I don’t think I understand testing as well a should. I have written a lot of tests and gotten decent coverage but I cannot help feeling it has not been intuitive.
Let me explain: if I have a class the I am testing a method and it needs to be passed a big object of some sort, with all kinds of state. This object in turn contains other objects and their states that I know nothing of, how do I create mock or stub objects for this method and give it data that it can work with. It seems I have to create a big object with all kinds of internal sub object information to exercise my method.
I’m clearly confused!
The other answers here are pointing you to mocking frameworks, which you should definitely look at if you’re not already using (use Mockito!). However, this is almost certainly an instance of your tests telling you that you’ve got design problems. If you find yourself having to provide all kinds of unrelated information and mock objects just to make a test pass, then you’re
These are all symptoms of a system not designed for testability, which almost universally equates to a system not designed for readability, meaning it’s not designed well.
If you care about testing well, embrace test-first thinking and TDD. I highly recommend a couple of books on the subject: “xUnit Test Patterns”, which I’ve read and reviewed, and “Growing Object-Oriented Software, Guided by Tests”, which I’m almost finished reading.