Just some initial thoughts on this question (which may explain why there is still no answer after a full hour!?;)
There seem to be two parts when it comes to resolving the issue:
1 / return all classes. Easy, you name jar, the Junit test initialization method:
- check that this jar is in the junit runtime class path
- read and load all classes in it
- remembers only those for which equals () and hash () are declared and redefined (via Reflection)
2 / check all objects
... and this is the catch: you must create instances of these objects that create two instances, and use them for equals () tests.
This means that if your constructors accept arguments, you must consider
- for primitive argument types (int, boolean, float, ...) or String, each combination of limit values (for String, "xxx", "", null; fonr int, 0, -x, + x, -Integer.MIN , + Integer.MAX, ... etc.)
- for non-primitive types, create an instance of those that will be passed to the constructor of the test object (which means that you must recursively take into account the constructor parameters of this parameter: primitive types or not)
Finally, not all parameters automatically created for this constructor will make sense in a functional sense, that is, some of these values will not be able to create an instance due to Assert: this must be detected.
However, it seems like it is possible (you can do this code-challenge if you want), but I want to allow others first. StackOverflow readers respond to this problem as they can see a much simpler solution that I have.
To avoid the problem with combinations and to ensure that the relevant test values are close to the actual code itself, I would recommend defining a highlighted annotation with a string representing the valid values for the constructors. It would be located above the overridden equals () method of one of your objects.
These annotation values will then be read, and instances created from them will be merged to test equals (). This would allow the number of combinations down enough
Side-node: JUnit's general test case, of course, will check that for each equals () there are for tests:
- some annotations as described above (unless only the default constructor is available)
- the corresponding hash () method is also overridden (if not, if he would throw an assert exception and fail in this class)
VonC Oct 10 '08 at 4:18 2008-10-10 04:18
source share