Late addition to the topic, sharing newer solutions
I am also looking for the right and efficient way to test (based on JUnit) the correctness of several implementations of some interfaces and abstract classes. Unfortunately, neither the JUnit @Parameterized tests, nor the equivalent TestNG concept meet my requirements, since I do not know a priori the list of implementations of these interface / abstract classes that may exist. That is, new implementations may be developed, and testers may not have access to all existing implementations; therefore, it is inefficient that test classes indicate a list of implementation classes.
At this point, I found the following project, which seems to offer a complete and effective solution to simplify this type of test: https://github.com/Claudenw/junit-contracts . Basically, this allows you to define "Contract Tests" through the @Contract(InterfaceClass.class) annotation @Contract(InterfaceClass.class) on test contract classes. Then the developer will create an implementation-specific test class with annotations @RunWith(ContractSuite.class) and @ContractImpl(value = ImplementationClass.class) ; the engine automatically applies any contract test that applies to the Class implementation by searching for all contract tests defined for any interface or abstract class from which the ClassClass implementation comes from. I have not tested this solution yet, but it sounds promising.
I also found the following library: http://www.jqno.nl/equalsverifier/ . This satisfies a similar, albeit much more specific, need, which asserts that the class conforms, in particular, with the contracts Object.equals and Object.hashcode.
Similarly, https://bitbucket.org/chas678/testhelpers/src demonstrates a strategy for validating some of the core Java contracts, including Object.equals, Object.hashcode, Comparable.compare, Serializable. This project uses simple test structures, which I believe can be easily reproduced to meet any specific needs.
Good thing for now; I will keep this post updated with other useful information I can find.
jwatkins Aug 6 '15 at 15:56 2015-08-06 15:56
source share