As JUnit Recipes reports, this is quite simple:
- Start from the test case for one of the implementation (I assume you have them, don't you?)
- Create a new abstract test case in which you will define the expected behavior
- Have your test case extend the abstract test case
- Move the code that instantiates the object under test into a separate method, storing the object as a reference to the interface and not the implementation.
- Adjust the rest of the code referring to the interface behavior accordingly
- Create an abstract creation method for each concrete creation method
- Move all tests for the interface to the abstract test case
I tried it all in JUnit 4... and it didn't work, as when I ran the tests for all the project I got a wonderful java.lang.InstantiationException (actually several of them) for the abstract test case.
After some personal tries and a bit of googling I found that all I needed to do to have everything run smoothly was to add the @Ignore assertion to the abstract test case. Now we have no excuses!
No comments:
Post a Comment