A reply to the JUnit mailing list about AbstractTestCases

Introduction


There was just a question posted to the JUnit mailing list about how to implement the AbstractTestCases pattern in JUnit4. Link here

I found the topic interesting and decided to write up my first wiki article about the topic. Here goes a possible implementation of the pattern...

The original issue as I understood it was...



  • A BigInterface: A interface with alot of methods.

  • Classes A, B, C, ... : Which implement the BigInterface

  • A test class per group of methods in the interface that calls A's, B's, C's implementation of that method



My implementation


So I created a simple interface to illustrate my point... Imagine this interface having a redonkulous number of methods. For instance the foo method would stand for several sets of methods. Thus, breaking up test classes into a set of like tested methods.


Implementing BigInterface

Consider the following as one of a set of classes each of which would implement a varied implementation of the BigInterface.

One could imagine the B's and C's implementation all being slightly different.
Solving the question, with a Parameterized test runner.

Thinking about this issue the Parameterized test runner came into mind. This is that implementation.

Bar's test would look similar with it's test methods matching it's grouping.

As one can clearly see, each implementation is added the to @Parameters method. This method provides a set of objects to pass into the constructor of each test runner. For our discussion they are implementations of the BigInterface.

Finally, we place each of the Parameterized tests in a suite.



The suite now runs all implementations of the interface as requested.

3 comments:

Unknown said...

Thanks for you article.

Cheers
Federico

P.s.
The idea was to split the big test class into small methods subsets, not one for class. :)

Big Mike said...

I've modified my original post that Federico commented on. I changed the post to take into account his comment.

Pete said...

This is exactly what I was looking for, thanks!