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

Icon

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.

  • No labels

6 Comments

  1. Anonymous

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

    why?

    (coverage do not appear on any plugin description too)

    1. Anonymous

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

    2. Anonymous

      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. Anonymous

    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. Anonymous

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

  4. Anonymous

    Is there any tool that I can incorporate in a Java project of mine such that it will work the same as what 'test with coverage' works in IntelliJ ?

    Basically, in my project, I want to check what percentage of code in so and so class has been covered by unit tests. That's why if there is a software/tool that I can use in my project that gives me this result (which is the same as 'test with coverage' feature of Intellij), then it will help my project a lot. 

Write a comment…