Like the TDD newb, which first encountered this situation for the first time, I found that as a result of refactoring I got "unchecked" inner classes, and if I use the "clean" TDD approach, I wonder if it can end up with inner classes in any other way .
The problem is that assuming that one or more references are made from an inner class for an external class object, this refactoring often interrupts one or more tests. The reason for this is quite simple: your layout object, if spy
, is actually a wrapper around a real object
MyClass myClass = spy( new MyClass() );
... but inner classes will always refer to the real object, so it often happens that trying to apply mocks to myClass
will not work. Worse, even without bullying, there is a high probability that this thing will completely and inexplicably fail in its normal business. Also keep in mind that your spy will not run the real constructor method for itself: many errors.
Considering that our development of our tests is an investment in quality, it seems to me that it would be terribly embarrassing to just say: "Well, I'm just going to refuse this test."
I offer two options:
if you replace your inner class with direct access to the outer fields of the class using getter / setter methods (which may be private
, rather strange), this will mean that these are mock methods that will be used ... and therefore false fields. Then your existing tests continue.
Another possibility is to reorganize this inner class to make it a free-standing class, an instance of which replaces your inner class and transfers one or more test methods to a new test class for this new class. Then you will encounter the (simple) task of fitting things so that references to the external object of the class are parameterized (that is, 99% of the cases are passed as a constructor parameter) and then can be mocked accordingly. It should not be too complicated. Although you may need to add the appropriate getter / setter methods for private
fields in the outer class and / or make one or more of its private
package-private
methods. From now on, the inner class becomes a black box for testing the outer class.
Using any approach, you have not lost quality.
source share