Search code examples
javaunit-testingtestingintellij-ideajunit

What is the scope of Test Coverage in Unit Test?


Suppose that I have demoServiceImpl and demoServiceImplTest in the following Maven project structure.

|--- src    
    |--- main
        |--- java
            |--- com.demo.services
                |--- demoServiceImpl.java

    |--- test
        |--- java
            |--- com.demo.unitTest
                |--- demoServiceImplTest.java

In IntelliJ IDEA, there is an option called Packages and classes to include in coverage data used to narrow down the code coverage scope.

I am wondering, for example if I want to see the coverage for demoServiceImplTest, which packages should I add to this section? I thought that, as I wrote this demoServiceImplTest Unit Test for testing just demoServiceImpl, then it is ok to add only demoServiceImpl or its package to this Packages and classes to include in coverage data section. Is that true? Or what is the proper approach?


Solution

  • You don't have to move any packages. Go to your class demoServiceImpl (btw that should be named DemoServiceImpl) inside package com.demo.services.

    Then hit Ctrl + alt + T (or menu -> Navigate -> Test). It will enter in right test class or ask you to create one to the right package with this popup : Create new test

    So, let the IDE automatically create the test class to the right package. When you will execute with coverage It will rely on it to check covered packages, classes and methods.

    Feel free to explore Injellij Documatation which is not bad at all. https://www.jetbrains.com/help/idea/running-test-with-coverage.html

    And : https://www.jetbrains.com/help/idea/create-tests.html