Writing unit tests on Legacy Code is an adventure. Today I spent several hours doing that with two developers, Mark and Yelena.
The system has a flow you are used to and when you write code you fit it into this flow. Then you test the entire system.
With unit tests it works differently. You are required to start the flow from the middle and stop it when your deed was done.
This requires a different level of system understanding. When we started off in the morning we looked at the code and thought “how are we going to harness this?”
To instantiate the class we needed a session so we extracted the code to a different place, then there was a logging mechanism we had to bypass. Everything was almost working when we realized that instead of getting a specific exception we got a null pointer. After additional investigation we found that the exception itself throws the null pointer because it didn’t get the file access it required.
On the way we found defects, it was a pleasure.
The main achievement was to reduce the level of anxiety from doing this. The more you practice it the faster it will go and you will become a true master of the system. Do not hesitate – take that adventure!