Setting Up JUnit in IntelliJ IDEA and NetBeans - Pragmatic Unit Testing in Java 8 with JUnit (2015)

Pragmatic Unit Testing in Java 8 with JUnit (2015)

Appendix 1. Setting Up JUnit in IntelliJ IDEA and NetBeans

In this appendix you’ll learn how to get JUnit unit tests running in both NetBeans and IntelliJ IDEA. The screenshots and IDE configuration steps you’ll see here supplant those in the Eclipse-based setup instructions starting at Learning JUnit Basics: Our First Passing Test. Note that these instructions cover configuring JUnit from scratch in the IDE and do not presume use of Maven or any other configuration tool.

For either IDE, first set up a Java project as you normally would. Then add the following source.[41] Make sure the package and directories match—both source files should end up in the iloveyouboss package within a source directory named src/iloveyouboss.

iloveyouboss/1/src/iloveyouboss/Scoreable.java

package iloveyouboss;

@FunctionalInterface

public interface Scoreable {

int getScore();

}

iloveyouboss/1/src/iloveyouboss/ScoreCollection.java

package iloveyouboss;

import java.util.*;

public class ScoreCollection {

private List<Scoreable> scores = new ArrayList<>();

public void add(Scoreable scoreable) {

scores.add(scoreable);

}

public int arithmeticMean() {

int total = scores.stream().mapToInt(Scoreable::getScore).sum();

return total / scores.size();

}

}

IntelliJ IDEA

You first need to install JUnit support. Navigate to IntelliJ’s Preferences dialog box and select Plugins from the left-hand menu. Scroll down to JUnit in the Plugins list and ensure that the corresponding check box is checked.

images/idea/plugins.png

Click OK.

Next, you need to download the JUnit library using Maven sources. Navigate to the Project Structure dialog box for your new iloveyouboss project (File ▶ Project Structure). From the left-side menu, select Project Settings ▶ Libraries. From the middle pane, click the + button to add a new project library:

images/idea/project_libraries_from_maven.png

From the Download Library From Maven Repository dialog box, you can either type the appropriate version of JUnit or use the search button to locate it. For our iloveyouboss example, we’re currently using junit:junit:4.11:

images/idea/download_junit_from_maven.png

Click OK.

You next need to set up a test directory in the project. From the Project window in IDEA, select the project and right-click to bring up its context menu. Select New ▶ Directory and enter test as the directory name.

Select the test directory from the Project window. From the context menu, select Mark Directory As ▶ Test Sources Root.

Open an editor on the ScoreCollection.java source file. Bring up the context menu and select Go To ▶ Test:

images/idea/goto_test.png

You see a tiny dialog box with the title Choose Test for ScoreCollection (0 found). Click where it says Create New Test…. IDEA presents you with the Create Test dialog box.

images/idea/create_test.png

In the Testing library radio-button group, select JUnit 4. In the check-box list labeled Generate test methods for:, make sure only arithmeticMean():int is selected. Click OK. IDEA should generate the ScoreCollectionTest.java source file in the test source directory in the iloveyouboss package:

images/idea/test_source.png

In the testArithmeticMean test, add a statement that calls the fail() method.

The discussion of Eclipse setup has a detailed explanation of the important bits of the test code. See Understanding the JUnit Test Bits.

You have a few choices for running the test. We prefer to run all the tests, so click the project name (iloveyouboss) from the Project window. From the context menu, click Run ‘All Tests’. You should see the JUnit Test Results window open:

images/idea/test_failure.png

Remove the fail() statement and rerun the test. You can do so using the menu item again, or via the keyboard with Ctrl-Shift-F10, or by clicking the green-arrow icon in the JUnit window. You should see a successful test run:

images/idea/test_success.png

At this point, return to Arrange, Act, and Assert Your Way to a Test to complete the exercise.

NetBeans

Navigate to the project properties page. Add a test package folder to correspond to the source folder; name it test:

images/netbeans/project_properties.png

Click OK to save the changes, then click File ▶ New File… from NetBeans’ main menu to open the New File dialog box:

images/netbeans/new_test_class.png

Select Unit Tests from the list of categories, then select Test for Existing Class from the list of file types. Click the Next button. You’ll see the rather busy New Test for Existing Class dialog box.

Enter the name of the class to test as iloveyouboss.ScoreCollection (or let NetBeans fill in the name by selecting the corresponding source class using the Browse… button). Then uncheck all of the check boxes in the bottom half of the dialog box, with the exception of the button marked Public that’s in the Method Access Levels group. Your completed dialog box should look like this:

images/netbeans/new_test_for_existing_class.png

When you click Finish, NetBeans creates iloveyouboss.ScoreCollectionTest:

images/netbeans/default_test_code.png

The author Javadoc comes from a NetBeans template. You can change this by navigating to Tools ▶ Templates, selecting Unit Tests ▶ Test Suite - JUnit 4.x from the list of templates, and clicking the Settings button.

Finally, make a few changes to the source file:

· Remove the testAdd() method (and the @Test annotation preceding it).

· Remove the Javadoc unless you have a burning need for it.

· Remove the constructor.

· In the testArithmeticMean test, add a statement that calls the fail() method.

The discussion of Eclipse setup has a detailed explanation of the important bits of the test code. See Understanding the JUnit Test Bits.

To run the test, click Run ▶ Test Project (iloveyouboss) from the NetBeans main menu. You should see the JUnit Test Results window open:

images/netbeans/test_failure.png

Remove the fail() statement and rerun the test. You can do so using the menu item again, or via the keyboard with Ctrl-F6, or by clicking the double-green-arrow icon in the Test Results window. You should see a successful test run:

images/netbeans/test_success.png

At this point, return to Arrange, Act, and Assert Your Way to a Test to complete the exercise.

Footnotes

[41]

Also downloadable from https://pragprog.com/titles/utj2/source_code.