In this post I would like to share some of the tips I gained by reading initial part of Junit Recipes book
and the blog http://misko.hevery.com/
- Testing follows three important acts Arrange,Act,Assert (create an object,invoke method on object ,Assert the value returned is as expected)
- The ATOM or smallest possible test is testing the return value of Method with the expected
- Testing should be focused on testing the Behavior of the object rather than testing Individual methods(Testing that the behavior change of the object being tested is as expected by applying the methods)
- Testing should be focused on What the Method does rather than the Implementation.In this way,tests are not coupled to implementation and hence easy to Refactor both the Code and Tests
- Value Objects Testing: Value Objects represent objects passed in the application.For example we can consider List as Value Object in the Java. In the case of Value Object it becomes important that both the Object equals method and hashcode ,as the correct implementation of equals method guarantees the Value objects (representing the same value is interpreted as same )..:)
- GSBase Equals Tester can be used to test whether the equals and hashcodemethod for an Object has implemented both these methods correct http://gsbase.sourceforge.net(com.gargoylesoftware.base.testing.EqualsTester).(Checks whether the equal method satisfies all properties reflexive,symmetric,transitive)
- For Testing methods that has void return values observe the side effect as the result of executing the method.If the method does not cause any change to the Object neither returns a value,then the Method does not need to be existed.
- If it is not possible to observe the side effect then try exposing side effects through method.In the long run,making these trade-offs leads to maintainable,testable code
- For Testing the constructor,tests can be written as to whether parameters passed to the constructor are injected appropriately to the object
- If the Object being constructed does not expose any readable properties,then assert the Object being constructed is in Valid state(isValid()).The same technique can be used to test the JavaBean too.
- Getter Methods do not warranty test generally unless the getMethods performs some processing on the incoming value.
- Setter methods can be tested by asserting the values of corresponding getter methods
- Singleton(or) Global state is evil.since it becomes very difficult to assert the state of the object being tested is expected to be in.