Search code examples
classunit-testingtestingtddmethodology

TDD should I create an empty class needed for a test case?


I'm new to TDD and am keen to start using it, but I keep hitting a bump whenever a test case I'm working on requires a class that doesn't yet exist (either as an input or as an output).

The problem is that I don't know whether to create the class without any functionality (is that considered untested code or not ?), or to stop working on the test (while it's green), and start writing a test for this new non-existing class.

The second approach seems to be recursive, and might cause me to lose my train of thought, while the first creates a new class for which there's no test.

Is there a third approach I didn't think of, which is preferable ?


Solution

  • After consulting several colleagues on this, the answer just came up to me !

    When testing class A and discovering mid-test that I need a non-existing class B, I'll create B as an interface instead of as a class. This will solve the problem (i.e. an interface cannot/needn't be tested) as well as contribute to better separation and abstraction.