Skip to end of metadata
Go to start of metadata

This tutorial will show how to use IntelliJ IDEA for developing unit tests and analyzing coverage.

Redirection Notice
This page should redirect to

1. Create new project from scratch

Create a Java project from scratch with the name UnitTestingApp.

2. Create a class to test

Now let's create a class, which we are going to test.

Add method sayHello which returns a "Hello" string.

3. Create a test source root

Instead of adding tests to the sources, you can mark any directory as a test source root, so tests will be separated from the production code.

Create a directory and mark it as a test source root.

4. Create a test class

IntelliJ IDEA provides a shortcut Cmd + Shift + T for navigating between classes and its tests. It also lets you to create a test class from right there.

Select JUnit 4 as the library for unit testing. IntelliJ IDEA will offer to add this library to the module. Choose to generate setUp method, and test for sayHello method.

If the project has several source roots, the IDE will ask you to choose one to put new test class in.

When the test class is generated, we can add code for our test method testSayHello.

5. Run test

Now we can run our test via the context menu Run 'MyClassTest' on the class, or via Run → Edit Configurations.

Results will appear in theRun tool window.

6. Edit tests configuration

We can adjust the run configuration of our tests via Run → Edit Configurations or via the toolbar.

On theConfiguration tab you can choose which tests must be run. For instance, you can run all tests from a class, package, test suite or even by pattern. Fork mode is here to let you run each test in a separate process.

On theCode Coverage tab you can adjust coverage settings. Currently IntelliJ IDEA supports two engines for measurement coverage. By default it uses own engine. Optionally, you can choose theJaCoCo engine. Also you can select coverage mode here. Tracing mode adds overhead but is more accurate in measurement.

7. Run with coverage

To collect coverage, you need to run tests in special mode via Run → Run 'MyClassTest' with Coverage or action on the toolbar.

After at least one test run in coverage mode, the IDE shows coverage data in theProject tool window for each package and class, as well as in theCoverage tool window, and right in the editor.

8. Coverage in the editor

If you add another method to MyClass and run tests with coverage, you will find that the code not covered by any tests is highlighted with red. The covered code is colored with green. If some code is covered only partly, it will be highlighted with yellow.

9. Download the source code

You can download the source code for this tutorial from GitHub.

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. Sep 17, 2014


    on a Mac: no way to get the "coverage" tab ?


    (coverage do not appear on any plugin description too)

    1. Sep 18, 2014


      Coverage is only available in utlimate edition. Maybe you are using CE?

    2. Dec 27, 2014


      Even if it's an ultimate edition, you need to enable the plugin via File => Settings => Plugins => Check "Coverage" in the Plugins list on first use.

  2. Dec 27, 2014


    Even if it's an ultimate edition, you need to enable the plugin via File => Settings => Plugins => Check "Coverage" in the Plugins list on first use.

  3. Mar 27, 2015


    Its available to CE too. Just add coverage plugin from File  => setting => Coverage. You can use EMMA or Clover for coverage.

Add Comment