Test Driven Development
- The importance of writing tests
- They force the developer to think of the class design
- They provide a safety harness while refactoring
- They ensure that the state of code is always stable
- New developers can make changes comfortable with the knowledge that if they break something that was working, the tests will inform them
- Test after development is not the same as test first development. Test after development does not reap all the benefits of test first development
- Writing the tests
- Think about the class it’s responsibilities and it’s API
- Write the tests to test every method and various conditions in the methods
- Whenever you think of writing a print statement or generate a log, it might be a scenario to include a test
- Write enough production code to ensure that the tests compile and fail
- Write production code to pass all tests one by one, while also ensuring that previous tests do not fail
- What not to test
- Database entities need not be tested
- Do not go overboard with tests. First write tests that are most likely to fail. Think of the cost benefit ratio while writing tests
- The test class
- Tests classes end with the work Test. The test class for Account will be AccountTest
- Test methods begin with the word test. The test method for creditAccount() will be testcreditAccount()
- Tests can either exist in a different source tree in the same package as the class they are testing, or in the same source tree, but in a different package.
- Managing dependencies
- Unit tests should ideally not have any dependencies
- Dependencies can be eliminated with mock objects (so that units can be tested in isolation)
- AllTests
- There should have one class AllTests that will run the entire test suite
- JUnit 3.8 for JDK 1.4 and before
- JUnit 4.x for JDK 1.5 and after
- StrutsTestCase for testing Struts Action classes
- http://www.testdriven.com
- http://www.xprogramming.com
- http://www.extremeprogramming.org
Requirements (Java Unit Tests):
References:
Notes: This text was originally posted on my earlier blog at http://www.adaptivelearningonline.net
Comments