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();
        }
    }
}


Add Comment

Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
Standard emoticons like :-) and ;-) are converted to images.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications
 
Submitted comments will be subject to moderation before being displayed.
 

About

My name is Mike Haller and I'm a software developer and architect at Bosch Software Innovations in Germany. I love programming, playing games and reading books. I like good food, making photos and learning and mentoring about the craftsmanship of commercial software development. Stack Overflow profile for mhaller

Quicksearch