0

I am not sure I still get how to "unit" test legacy code such as this...

Most of my code is to put things and remove things from an object of class A that has a number of other objects inside it and so on like a tree. At the leaf of this tree is a data item object backed by a OR mapped database row... Creating a real object of classA is a non trivial task since it involves creating all the nodes of this tree. I dont even have visibility into the classes beyond one level inside A.

My code needs be exercised when the classAobj is state X and this code will move classAobj to state Y

What would be the best way to get classAobj into state X ?

I have heard Mock objects and Stub objects bandied about but not sure about how I am going to handle anything with deep expectations and how can I create a stub when I need all of the tree ?

How do you discover what other objects are needed for classAobj to function ?

treefrog
  • 1,027
  • 1
  • 15
  • 30
  • Please improve your question by posting just the relevant sections of your code, [properly formatted](http://stackoverflow.com/editing-help). In addition, please post any actual error message you received, and what steps you've taken so far to research or resolve your programming issue. – Todd A. Jacobs Jun 09 '12 at 14:13

1 Answers1

0

Adding unit tests to legacy code not designed for unit testing is very hard and you may end up writing tests that really just want to junk a short while later as you refactor that entire object tree.

I'd look at writing integration tests as this will give you the most freedom to refactor but at a large cost of setup.

Michael Lloyd Lee mlk
  • 14,561
  • 3
  • 44
  • 81