tags:

views:

250

answers:

3

Our application depends on numerous resources that are provided by another system.

To ensure the existence of those resources, we currently have a JUnit test case (probably more an integration test), that takes a list of all the resources as a textfile, fetches each and tracks success/failure.

This is a very long running testCase that is not very "tool friendly". What we would really like to have is something along the lines of one test-method per resource.

I am aware that this is not what JUnit was meant to do. But is there a way to generate those testmethods on the fly?

Maybe something a bit more "elegant" than writing a perl-script to generate hundreds of methods?

Thanks a lot!

A: 

You might want to take a look at the TestSuite class, and creating your own instance (rather than letting one of the junit runners just run all the tests in a certain dir) and/or subclassing it - the TestSuite has methods to programmatically addTests to it, and then you can run all the tests within the suite.

matt b
+4  A: 

You may want to look at parameterized tests. This is easier to achieve in JUnit 4, though can be done in JUnit 3. See this question for code: JUnit test with dynamic number of tests

McDowell
Jep! Perfect solution, because you could implement an FileInputStream into this paramerized tests to load the resource list to check.
furtelwart
In my oppinion this is a near perfect solution. In IDEA, the parameterized tests sadly collapse to a single one. This means we still don't see at once, which resources were not available. However thanks for the parameterized tests - I didn't knew them.
Mo
A: 

This: http://github.com/adewale/cq-challenge-markup/blob/b99c098f0b31307c92bd09cb6a324ef2e0753a0b/code/acceptance-tests/AcceptanceTest.java is an example of a class that dynamically generates one test per resource using the JUnit4 @Parameterized annotation

ade