Useless JUnit Tip #327
Posted by Mike Haller
on Monday, December 25. 2006
at 00:00
My today's useless tip is for Eclipse/JUnit. Sometimes, while developing, i'd like to test-drive my class. In former times, developers have often used the main() method, and often forgot about them. So there are some applications out where with a lot of not-intended main entry points. My genuine idea is to use JUnit and Learning Tests for this kind of code. Usually, you create a TestCase for your class-under-test and write tests which prints out stuff to just find out how something works. Now, you can embed a TestCase as inner class into its class-under-test like this:
public class Bar {
// Your code here ...
public class BarTest extends TestCase {
public void testCreate() throws Exception {
new Bar();
}
}
}
The problem with this approach is, then you hit Alt-Shift-X and T to run the TestCase in the JUnit Runner, it will probably fail with the error message "Class X has no public constructor TestCase(String name) or TestCase()":

The fix for this is rather easy: Make your TestCase static:
public class Bar {
// Your code here ...
public static class BarTest extends TestCase {
public void testCreate() throws Exception {
new Bar();
}
}
}
