0
votes

I'd like to create a "generic" JUnit 4 test that can be used as follows:

  • It is an abstract base class
  • It can be packaged into a jar file
  • Users can include the jar as a dependency in their own projects
  • Users can create their own JUnit tests by extending the abstract class and adding implementations for the key methods
  • The JUnit tests will then run when the users run JUnit on their own projects (along with any other JUnit tests the user may have written)
  • The test suite may return multiple results from running different sub-tests, ideally the user should be able to see this breakdown of results in their IDE

The motivation for this is that the class defines a standard test procedure that may be useful in many different projects, but require some customisation by the user in each case.

Is this possible with JUnit 4, and if so how should I do it?

2
You describe technical features, but not what your abstract test suite actually does. Maybe you really want to do s.th. with a JUnit Runner or JUnit Rules to extend functionality? Not sure based on your question. - mhaller

2 Answers

1
votes

Yes it's possible. You would simply have to create your abstract class, package it in a jar, give this jar to the users or store it somewhere where they can download it.

Users would then put the jar in their own project and create subclasses of your abstract class. They would use their IDEs (or any other way) to run these test classes extending your abstract class, and the IDE would display the result of all the tests, typically using a tree.

This looks like a repetition of everything you asked in your question, but the way to do it is just as you have explained in your question. There's not much to add.

1
votes

Perhaps this can provide you a starting point: Writing base class for your tests.
Just pack it as every other class in a jar and you are ready to go.