Let's tackle the likely concern that a class with the word "Test" in its name is not a junit.framework.TestCase subclass. A test for this case would need to specify a class that the SuiteBuilder can attempt to gather and then reject. You might consider using an existing class that you have already coded for the student information system, such as TestUtil. However, you do not want to depend on the existence or stability of existing classes. Someone making a valid change to TestUtil, or deleting it, would unwittingly break SuiteBuilderTest tests. You instead will create dummy test classes expressly for the use of SuiteBuilderTest. Start by creating the class NotATestClass in the new package sis.testing.testclasses. package sis.testing.testclasses; public class NotATestClass {} NotATestClass does not extend junit.framework.TestCase, so it should not be recognized as a test class. Add an assertion to your test method. public void testGatherTestClassNames() { SuiteBuilder builder = new SuiteBuilder(); List<String> classes = builder.gatherTestClassNames(); assertTrue(classes.contains("testing.SuiteBuilderTest")); assertFalse(classes.contains("testing.testclasses.NotATestClass")); } The relevant code in SuiteBuilder:[6]
public List<String> gatherTestClassNames() { TestCollector collector = new ClassPathTestCollector() { public boolean isTestClass(String classFileName) { if (!super.isTestClass(classFileName)) return false; String className = classNameFromFile(classFileName); // 1 Class klass = createClass(className); // 2 return TestCase.class.isAssignableFrom(klass); // 3 } }; return Collections.list(collector.collectTests()); } private Class createClass(String name) { try { return Class.forName(name); } catch (ClassNotFoundException e) { return null; } } The additional constraint implemented in SuiteBuilder is that a class filename must represent a compilation unit that extends from TestCase. There are three steps involved. The following descriptions correspond to the commented lines of code in gatherTestClassNames.
Take a brief look at the Java API documentation for java.lang.Class. It contains methods such as getMethods, getConstructors, and getFields that you can use to derive most of the information you need about a compiled Java class. |