27

So this question might not be really specific, but i'm asking it anyway.

I'm trying to use JaCoCo with IntelliJ to gather coverage reports on unit tests. However, i don't have any experience whatsoever to set this up.

In Netbeans you just have to install the JaCoCo plugin and select to test with coverage. How does this work in IntelliJ?

I have googeled for quite a while without success. If someone has a good tutorial to set this up, i'd really appreciate it!

Ixbitz
  • 427
  • 1
  • 5
  • 7

8 Answers8

23

To elaborate on jwenting's answer, go to Run > Edit Configurations..., and on the Code Coverage tab of your Run Configuration, select JaCoCo from the Choose coverage runner dropdown.

Run Configuration Screenshot

solimant
  • 809
  • 9
  • 15
  • 5
    note that this screen tends to change once in a while, for example in the 2021 version of IntelliJ the code coverage options are now below the configuration options, not in a separate tab. – jwenting Jun 10 '21 at 08:21
  • And make sure Java Code Coverage plugin is enabled. Otherwise no Code Coverage section is rendered. – Denis Rozhnev Aug 09 '22 at 16:35
17

You don't need to do anything, if you have a recent version of IntelliJ. Just select to use JaCoCo as the coverage tool in the run configuration for your test suite or project and it will use its built-in JaCoCo version.

jwenting
  • 5,505
  • 2
  • 25
  • 30
9

Thank you to all of the preceding answers. The following may be helpful for anyone using later versions of Intellij:

Community 2022.1 & Community 2022.2

RunEdit ConfigurationsModify OptionsSpecify alternate coverage runner

enter image description here

enter image description here

enter image description here

and finally ApplyOK

henry sneed
  • 356
  • 4
  • 14
  • Hi @DenisRozhnev thanks for the comment. I upgraded to Community 2022.2 on separate computers, one running Linux and the other Windows and the same steps still apply. Could you list the steps you are taking through the IDE so I can try to reproduce? – henry sneed Aug 03 '22 at 17:14
  • There is no "Code Coverage" section if Java Code Coverage plugin is disabled. – Denis Rozhnev Aug 09 '22 at 16:33
4

Have you looked at this document : https://www.jetbrains.com/help/idea/2017.1/code-coverage.html

They describe how to do such a task.

kgui
  • 4,015
  • 5
  • 41
  • 53
3

Intellij shows code coverage in the IDE. There is no need to install any extra plugins

when you right click on a Test.java file to run the unit test then there will be 3 options

Run "Test"
Debug "Test"
Run "Test" with **coverage**

Secondly, if you also want the branch coverage details then,

after running the test(as mentioned above) once, then then go to edit configuration -> at left side under Junit select the respective Test.java file -> select Code Coverage tab in the main window -> select Tracing radio button and tick the Track per test coverage checkbox

firstpostcommenter
  • 2,328
  • 4
  • 30
  • 59
3

With IntelliJ 2021.2.1 (and possibly prior to that), Jetbrains switched the default to a bundled code coverage runner they provide. But it does not provide branch coverage, as JaCoCo does. JaCoCo is not listed in Plugins, but it was available as an alternative for me (perhaps because it's in the Maven POM).

The solution above using Run -> Edit Configurations will change a single run configuration (e.g. for a single JUnit test class). To change it for all new ones, after going to Run -> Edit Configurations, click Edit Configuration Templates on the lower left, and change the coverage runner there.

Ron HD
  • 161
  • 9
1

To apply JaCoCo to all future coverage runs, you can modify the Junit template and next coverage runs use this coverage.

  1. Go to Run/Debug Configurations
  2. Click Edit Configuration templates...
  3. In Build and run, click Modify Options and select Code Coverage/Specific alternative coverage runner
  4. A new Code Coverage appear. Select JaCoCo
  5. Click OK or Apply button
  • 1
    Appears to be the same answer as henry sneed's, though shorter without screenshots. (Both might benefit from a version number) – user9712582 May 12 '22 at 03:09
0
  1. You have to open your Run Configuration

    1A. (top right by default) Tests in <project package>

    1B. If you can't find it, you can go to the Run menu at the top, then click the Run > ... option. This will show you your configurations.

  2. click Edit Configurations. Find your Tests in <project package> configuration.

  3. Find Second tab from the left is Code Coverage. Choose Coverage Runner: Jacoco.

  4. Apply. OK.

  5. Run tests again :).

Stevers
  • 516
  • 5
  • 11