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.

1. Create new project from scratch

Create Java project from scratch with name UnitTestingApp.

2. Create a class to test

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

Add method sayHello returning 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. In this case 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 right from there.

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

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

When 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 Run tool window.

6. Edit tests configuration

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

On Configuration 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 Code 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 JaCoCo engine. Also you can select coverage mode here. Tracing{span{ mode add 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 Project tool window for each package and class, as well as in Coverage 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 uncovered 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?

Add Comment