10

I have an AbstractTestBase and many junit Test classes extending it, and new Test classes are extending it over the time
I have this test suite now

@RunWith(Suite.class)
@SuiteClasses({ATest.class, BTest.class, CTest.class})
public class AllTests {}

ATest, BTest, CTest, ..., ZTest extend AbstractTestBase
How to create a junit test suite that dynamically runs all subclasses of AbstractTestBase without having to list them manually in @SuiteClasses using Junit, java-7 only, no 3rd parties.

I figured out the solution

https://gist.github.com/amgadhanafy/b172cf776bfe313762d38480e17f4517

Amgad Hanafy
  • 537
  • 5
  • 15
  • 1
    It sounds like your directions are clear. What are you asking us, then? – Makoto Mar 02 '18 at 17:26
  • We aren't really interesting in what you *need* though. What exactly is your question here? – M. Prokhorov Mar 02 '18 at 17:31
  • How to create a junit test suite that dynamically runs all subclasses of specific class using Junit, java-7 only, no 3rd parties. – Amgad Hanafy Mar 02 '18 at 18:17
  • Extending a class in Java is one-way relationship. Superclass doesn't (and shouldn't) know about its subclasses. The only way you can do what you want is by scanning the classpath, but even that is unreliable. – M. Prokhorov Mar 02 '18 at 18:36
  • I don't get your point. Any JUnitRunner I know of already scans the classpath and runs any method you annotated with `@Test` even if it is in a superclass or regardless how many super classes exist... – Timothy Truckle Mar 02 '18 at 22:50
  • @TimothyTruckle the superclass is updated frequently and I need to run all subclasses to make sure that nothing is broken after the update, at the same time, I don't need to run the entire test folder as it will take a long time. – Amgad Hanafy Mar 04 '18 at 11:32
  • @M.Prokhorov I figured out how to do it, please release the hold to add the solution – Amgad Hanafy Mar 04 '18 at 16:11
  • @AmgadHanafy *"the superclass is updated frequently and I need to run all subclasses to make sure that nothing is broken after the update"* Looks like you ignored the "Favor Composition over Inheritance" principle. You should improve your code and extract the common behavior to a separate class that you can test separately and exchange in your other test with a *test double*. That will improve the speed of your tests and avoid testing the dependent classes because of a change in the common class. – Timothy Truckle Mar 04 '18 at 21:37
  • 1
    @TimothyTruckle, JUnit Runner doesn't scan anything, it's out of scope for its responsibilities. It only searches for `@Test` or analog of it in the scope of classes it's given, and runs these tests. Scanning occurs as a separate thing by building a specific `org.junit.runner.Request`. And base JUnit system doesn't do any scanning at all: classes to run are passed to it as an argument. Closetst thing base JUnit has to scanning is `Suite` runner, which also needs a explicit class enumeration by default. JUnit5 may be different, I'm not familiar enough with it's inner workings. – M. Prokhorov Mar 05 '18 at 13:07
  • I did it in 2 steps 1- get all subclasses of the superclass 2- feed the list to test suite – Amgad Hanafy Mar 05 '18 at 15:07

0 Answers0