4.5 (Not) Testing GetSet Methods


4.5 (Not) Testing Get/Set Methods

Every behavior should be covered by a unit test, but every method doesn't need its own unit test. Many developers don't test get and set methods, because a method that does nothing but get or set an attribute value is so simple that it is considered immune to failure. Tests of such methods are correspondingly trivial, as shown in the test of getTitle( ) in Example 4-11.

Example 4-11. Trivial test of getTitle( ) method
 BookTest.java    public void  testGetTitle  ( ) {       Book book = new Book( "Solaris", "Stanislaw Lem" );       assertEquals( "Solaris",  book.getTitle( )  );    } 

If a get or set method produces any side effects or otherwise has nontrivial functionality, it should be tested . For example, with lazy initialization, a get method may compute an attribute value before returning it behavior that deserves a unit test.

Unit Test Frameworks
Unit Test Frameworks
ISBN: 0596006896
EAN: 2147483647
Year: 2006
Pages: 146
Authors: Paul Hamill

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net