dagblog.
← Archive

Eclipse tips: JUnit support


The Eclipse development environment has built-in support for the unit testing framework 
JUnit. It offers several useful features that make unit tests easier to create, run and evaluate.

Creating a test class

Eclipse makes it easier to create test classes with 
a simple wizard. Right-click the class you want to create a test 
for and choose New >> JUnit Test Case to open the wizard.

Opening the wizard

Eclipse menu for opening the JUnit wizard
Image could not be loaded. Eclipse menu for opening the JUnit wizard

If JUnit Test Case is missing from the New menu, you can enable it
under Window >> Customize Perspective >> Shortcuts tab >> Submenus: New >> expand Java and check JUnit.

The wizard

The first page of the wizard offers basic options based on JUnit’s features. For example, you can generate the setUp and tearDown methods or configure the test runner.

First page of the JUnit test wizard
Image could not be loaded. First page of the JUnit test wizard

Click Next to go to the page where you can select which methods of the class to test. The wizard generates test stubs for those methods.

Second page of the JUnit test wizard
Image could not be loaded. Second page of the JUnit test wizard

Creating a Test Suite

Eclipse helps you create a Test Suite just as it does a test class. Open the Test Suite wizard the same way: New >> JUnit Test Suite. All the test classes in the package are automatically added to the suite.

Wizard for creating a JUnit test suite
Image could not be loaded. Wizard for creating a JUnit test suite

JUnit view

The JUnit view gives you a graphical interface for watching test results and inspecting failures. If the JUnit view isn’t in your perspective, enable it via Window >> Show View >> Other >> Java >> JUnit.

JUnit test results panel in Eclipse
Image could not be loaded. JUnit test results panel in Eclipse

Running tests

The test launch interface is another feature that makes tests easier to work with. Right-click the test class and choose Run >> JUnit Test. You can also use Run >> Run As >> JUnit Test. The JUnit view displays the progress graphically.

Related articles

Medium version · Archived original page · Screenshots: Roman Pichlík, CC BY 3.0 · Historical Sweb link

Related articles

Related CZ Podcast episodes