![]() ![]() * Note also that array properties are ignored. * the value (since there isn't a public setter). Reflection allows programmatic access to information about the fields, methods and constructors of loaded classes, and the use of reflected fields, methods, and constructors to operate on their underlying counterparts, within security restrictions. Note specifically that read-only properties are ignored, as there is no way for us to know how to set Java Reflection provides classes and interfaces for obtaining reflective information about classes and objects. * automatically introspect the target object, finding read/write properties, and automatically testing the getter The following is all we have to do to test basic getter/setter behavior of an void testProperties() method. we create class A that extends class B, then use reflection to invoke sayGoodbye() method of class B from an instance of class A. Lets look at the below code example to see how it works. (As the mantra goes, "test everything that could possibly break.") This post shows you how to call or invoke methods of parent/super class by using Java Reflection. In languages that provide explicit support for properties this is a joke, but this is Java, let's just get over that and just test it. In any case I think that since it is possible someone could manually mess up a getter or setter, they should be tested. One of those cases is when using Hibernate as I described in an earlier post. There are exceptions of course: if a collection or map or array is returned sometimes you are expected (or required) to actually manipulate the returned mutable collection object. Most Java developers know that objects returned by getters are meant for reading, and if you want to change the value, explicitly call the setter. I've never once had a problem where a Date, which is a mutable object, returned by a getter was changed and caused weird issues. As a matter of practicality, it just doesn't matter most of the time. While true to an extent, I want to know how many Java developers really create defensive copies in getter and/or setter methods. :-) Tools like Findbugs consider this to be a code smell since the getter may be returning a reference to a mutable object, as does Josh Bloch in Effective Java. Some people think this is cheating or just playing around with test coverage numbers. So the other day I wrote a test utility that uses reflection to test that values passed to the setter methods are the values returned by the corresponding getters. But no way am I going to write individual tests for every single getter and setter method. Since getters and setters are explicit code in your application, I think they should be tested. But most people don't assert every single property in the course of testing their data access tier, so the actual behavior many times is not actually being tested. If you measure your unit test coverage, getters and setters are normally tested during the course of all your other tests, for example if you use an ORM tool like Hibernate it will call the setters when populating objects and the getters when reading object state to persist changes. The question is whether you should write explicit tests for these methods or not. Since Java has no notion native syntax for declaring properties you have to write explicit getter and setter methods, or hopefully you never write them but rather you have your IDE generate them. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |