Sunday, February 22, 2009

Test Driven Development - Just Mock It

There are number of software development methodologies exist with each having its strong and week points. These methodologies are applied to various projects depending on their requirements, degree of risk, time bounds and so many other facts. Test Driven development is one of the methodology to which software developers adapted recently. It says write the test first and write logic(real requirement) next.

But the problem is when you are working on large project which is not develop with test driven development in mind. It is not easy to apply these techniques directly to such project.This fact is very true if the project is on maintenance stage. Because in that stage you don't have to write whole method, you are just required to change some logic and test it, So you can't write test which will cover whole method you changed. It is great if you can test only the logic you changed. In java JMock is the solution for this.

JMock allows you to mock the objects inside the big method, set expectations and test only the part you change.Which will ultimately allows you to validate your logic. JMock is also very useful when you can't set the required environment to test your code eg - if you want to test your code on web server or application server. You can simulate this environment by mocking required objects(HttpSession, HttpRequest, local and remote services etc...)

Please refer for more information.