The first problem is that Test3 uses @RunWith(Suite.class)
, but doesn't contain @Suite.SuiteClasses({Test3.class, .....})
. This produces an IntializationError: class 'Test3' must have a SuiteClasses annotation
. Since you aren't intending there to be any classes underneath Test3, this annotation should be removed.
The second problem of Exception: No runnable methods
is almost always due to forgetting to add @Test
to a test. You didn't put the tests in your sample, so I don't know if that's actually the case or not, but it's the most likely cause.
The following is a working version of your code that allows tests to be run from any class:
Test1.java
import org.junit.runner.*;
import org.junit.runners.*;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({Test2.class})
public class Test1 {
}
Test2.java
import org.junit.runner.*;
import org.junit.runners.*;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({Test3.class})
public class Test2 {
}
Test3.java
import static org.junit.Assert.*;
import org.junit.*;
public class Test3 {
@Test
public void testTrue(){
assertTrue(true);
}
}
As for whether or not there is a better way than to organize things, I guess it just depends on how you decide to create the classes. Since you can add suites to suites, as this demonstrates, you can create smaller chunks of suites that depend on everything, like a tree. For instance, what I generally do is:
AllTestSuite
TextParsingSuite
GuiSuite
SwingSuite
JavaFXSuite
FileIOSuite
A test is added to the most relevant suite. In the end, I don't think I have any suite with more than 10 test classes/suites or so. If I do, it's time to make a new sub-suite. In other words, there is no "huge list somewhere", just a lot of smaller lists that are combined together into another list in order to effectively make one big list.
I suppose you could use some tool to dynamically find all of the Java classes containing tests, but JUnit itself doesn't support this behavior (it only runs the tests you tell it to, which I personally think is a good thing).