I find myself writing lots and lots of boiler plate tests these days and I want to optimize away a lot of these basic tests in a clean way that can be added to all the current test classes without a lot of hassle.
Here is a basic test class:
class MyClassTest { @Test public void doesWhatItDoes() { assertEquals("foo",new MyClass("bar").get()); } } Lets say if MyClass implements Serializable, then it stands to reason we want to ensure that it really is serializable. So I built a class which you can extend which contains a battery of standard tests which will be run along side the other tests.
My problem is that if MyClass does NOT implement Serializable for instance, we still have a serialization test in the class. We can make it just succeed for non-serializable classes but it still sticks around in the test list and once this class starts to build it will get more and more cluttered.
What I want to do is find a way to dynamically add those tests which are relevant to already existing test classes where appropriate. I know some of this can be done with a TestSuit but then you have to maintain two test classes per class and that will quickly become a hassle.
If anyone knows of a way to do it which doesn't require an eclipse plug-in or something like that, then I'd be forever grateful.
EDIT: Added a brief sample of what I described above;
class MyClassTest extend AutoTest<MyClass> { public MyClassTest() { super(MyClass.class); } @Test public void doesWhatItDoes() { assertEquals("foo",new MyClass("bar").get()); } } public abstract class AutoTest<T> { private final Class<T> clazz; protected AutoTest(Clazz<T> clazz) { super(); this.clazz = clazz; } @Test public void serializes() { if (Arrays.asList(clazz.getInterfaces()).contains(Serializable.class)) { /* Serialize and deserialize and check equals, hashcode and other things... */ } } }