In big companies, developing a green field project is a rare privilege
... most of the time you will have to develop on a existing code base of
versatile quality - Not due to the skill of the developer more likely to
the history of the project, lack of code review and poor quality focus,
short term vs. long term risk -.
Anyway if you are not lucky you are facing a big spaghetti monster
calling other pasta entities in ways ranging from CORBA, RV, rmi, EJB,
ftp messaging ... Chances is that the code can't even run on your dev
machine, making development laborious and non productive.
There are 2 ways to deal with this problem
1 Status quo, self explanatory
2 Introducing unit test, mocking the internal and external monsters
1 : simple solution, not very rewarding but if the cost of solution 2
overcome the benefit that's the way to go
2 : long term benefit in development and quality. But costly
Though with a bit of method there are way to make 2 less costly, all you
will need is :
- A good refactoring dev environment, eclipse or idea
- Junit, XmlUnit
- XStream an xml serializer/desiralizer
- an UAT/Dev environment that runs.
1 - identify the part the function you need to test
you won't be able to test all the code, it is important to test a small
chain of the code lest that the mocking will be too complex.
It's likely that you can identify the function that is the entry and
exit point
Object f1(Object... args)
It is that function that we will try to unit test.
2 - record samples
your system is probably generating high complex object, based on high
complex entry. Trying to reproduce the graph manually is laborious and
sure to be incomplete. That's what we need to used XStream to record the
args and the output of the function in a uat environment, getting that
way real life object.
// the new method that will delegate to the old
// and dump in and out
Object f1(Object... args) {
dumpIn(args);
Object o = _f1(args);
dumpOut(o);
}
// was the real
Object _f1(Object args) {
...
}
3 - integrate your sample in your unit test
Now that you have sample with your input parameters and your expected
result you can start writing your unit test.
4 - mock the external call
It is likely that once you integrate the sample in the unit test, the
code will need to code external or internal component. you will need to
refactor that code in specific method that you will overwrite for your
test.
class F {
Object _f1(Object... args) {
// used to be inline
Object extInf = getExtInfo();
}
Object getExtInfo() {
....
}
}
class FTest extends F {
Object getExtInfo() {
return new MockObject();
}
}
If the object is to complex to Mock you can use the XStream to dump a
sample in uat environment.
5 - run the test !
6 - how to improve the solution
It is possible to create the list of test automatically by scanning the
samples directory e.g.. Making it easier to add sample and extend the
coverage of the test and behaviour.
This is an iterative process, it is not possible to make everything
testable in one go, take your time and surely step by step the quality
of your code will improve.