Reputation: 1938
I need to test some variables of complex object eg.
Object - Person |->Name |->Address | |->Street | |->City | |->Zip | |->Apt_num |->Interests | |->Sports | |->Music | |->Movies |->Other1 | |->x1 | |->x2 |->Other2
For above objet I want to write unit tests which will test different variables of Person object eg. city, zip, apt, x1, etc. In above object Name, Address, Interests etc are also objects and some other objects. Child objects in person can be null. I am using testng to write test. Is there a good way by which I can write dataProvider for above object and tweak variables.
Upvotes: 0
Views: 600
Reputation: 1117
In pure unit testing you should test one class, e.g. Person, completely separated from other and test methods of your unit for all (edge) cases.
Completely separated is very important thing as you want to do unit testing so you should test only one unit - e.g. person class.
What about all dependencies? You should mock them as you must be 100% sure that they work according to your requirements - it could mean:
Sometimes it's very hard to achieve those requirements from outside. I mean you can't be sure that someone does not commit a piece of code which will not return null
instead of address's street or it can be hard to set Interest's music to null as the API does not have to expose such method.
Mocking is the right thing you should use. Look at example http://en.wikipedia.org/wiki/Mockito as you can completely replace dependencies of your component in your test in few code lines with your expected behavior.
Upvotes: 1