Example for writing UnitTesting[UT]:
In Android to automate test cases, InstrumentationTest Runner is available.
One new guy has modified some code, it is affecting some functionality. This error was captured while running the automated test cases.
In my work environment, Once the code is committed to repository, Hudson server triggers the build, test for automated Unit testing cases. If all the Unit testing cases are passed, then only it will allow us to submit.
To maintain legacy code for a longer period of time, it is best to write automated Unit testing,This will maintains product quality from the situation where the fresher/ new developer has added code without understanding.
In case of UT, we can mock objects instead of testing with real object. I encountered the problem in getting an IP address when wifi and wifi direct is on.
So To solve this problem, I mocked the wifi, wifi direct scenario in UT cases, so that it wont never happen again and if some one is modifying the source code, his source code should pass my testcase,Then only he can submit the source code to repository.