11

I have a Maven test project for my application.

The JUnit tests run fine, and the code coverage test run too.

But the report always shows 0% of code coverage.

What should i do?

Josh Kelley
  • 56,064
  • 19
  • 146
  • 246
renanleandrof
  • 6,699
  • 9
  • 45
  • 67

8 Answers8

5

According to the official site, Eclemma is a code coverage plugin for Eclipse, based on JaCoCo library.

As you want to use the same code coverage engine outside eclipse, you should include the plugin Jacoco inside the Maven configuration (pom) of your project, as the following (this code was copied from the Agile Engineering blog):

<build>
    <plugins>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.6.0.201210061924</version>
            <executions>
                <execution>
                    <id>jacoco-initialize</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <execution>
                    <id>jacoco-site</id>
                    <phase>test</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

To run the tests just type the following on the command line tool:

mvn clean test

p.s.: you also could use other code coverage plugins like Cobertura or Emma.

eis
  • 51,991
  • 13
  • 150
  • 199
  • I couldn't insert the hyperlinks for the [Eclemma](http://www.eclemma.org/) and for the [Emma websites](http://emma.sourceforge.net/maven-emma-plugin/) on the post above. – Joao Piccinini Jan 02 '13 at 02:37
  • I also got same problem, I got a GWT project and not maven..., as Renanlf said always am getting 0% coverage for everything. – Dipak Jan 10 '13 at 08:45
2

Just in case you forgot to do these:

  1. Are you annotating your tests using @Test?
  2. Are you running the class as a JUnit test case or from the coverage button?

I'm not sure what the cause of the problem is, cause it always worked for me. Have you installed it from eclipse itself? Try to uninstall it, and reinstall from eclipse. Here's how to do it just in case:

  1. In Eclipse, Click "Help" > "Install new Software"
  2. Click "Add", and type the following:
  3. Select EclEmma, and install
Steve
  • 1,469
  • 2
  • 11
  • 11
  • Yeap, using @Test. Running from the Coverage Button. The tests run fine, but the coverage always show 0%, all red paths. – renanleandrof Dec 19 '12 at 17:18
  • Nope... still the same =/. Do you use it on a maven project? Cause the problem only seems to happen on maven projects here – renanleandrof Dec 21 '12 at 13:59
  • I don't think so, I don't even know what maven is. – Steve Dec 26 '12 at 06:57
  • 2
    I had a similar issue and it was because I had annotated the class with the @RunWith(PowerMockRunner.class) - once I change it not to need this, then the code coverage came back again – Rob Wilson May 22 '19 at 22:51
2

Now I realized that you just want to get a report using the tool inside Eclipse...

How is the code coverage in the Eclipse Dialog? Did you tried to use the mouse right click on this dialog to export session (report), or inside File -> Export?

  • Yeah, this is only to export an execution of the coverage, the report exported continues with 0% coverage =/ – renanleandrof Jan 02 '13 at 12:17
  • So, I have two suggestions: 1. Try to reinstall the Eclemma plugin (if you used the update site to install it on the last time, try to install it from marketplace now); 2. If the previous didn't work, try to use JaCoCo library directly from Maven (as I explained in the other post) to isolate the problem, making sure that the problem is only about the layer above Jacoco (eclipse plugin) and not from this engine. – Joao Piccinini Jan 02 '13 at 15:44
2

It's a known issue for many years and unfortunately there's no official solution yet for it.

You can see it here, here and here

One not-so-honey solution might be to try using eCobertura (or downgrading eclemma from 2.x to 1.x)

KrishPrabakar
  • 2,824
  • 2
  • 31
  • 44
1

If you are using eclemma, you need to add jacoco dependency. if jacoco has been added and still, you are facing this issue, refer the eclemma faq: "Why does a class show as not covered although it has been executed?"

it says,

First make sure execution data has been collected. For this select the Sessions link on the top right corner of the HTML report and check whether the class in question is listed. If it is listed but not linked the class at execution time is a different class file. Make sure you're using the exact same class file at runtime as for report generation. Note that some tools (e.g. EJB containers, mocking frameworks ) might modify your class files at runtime.

So, Mockito / PowerMockito can cause this problem. In my case, I have added the class in @PrepareForTest(). I was shown that the test case was executed fine without errors but Jacoco did't improve the code coverage in its report.

Finally, removing the class from @PrepareForTest() annotation improved the code coverge. check if you have added it or not and remove it from annotation if added.

swaroop
  • 321
  • 4
  • 5
0

I just came across this issue and it was caused by an incorrectly configured classpath. When the unit tests were executed, they were executing against a compiled jar (actual source compiled outside of eclipse) and not my actual source code. After removing the jar from my classpath, the unit tests correctly hit my package source.

slocumro
  • 258
  • 1
  • 10
-1

I was able to resolve the issue on mine by calling a instance of the class at the top of the test cases. i.e.

public hotelOccupancy hotel = new hotelOccupancy();

@Test
public void testName() {
    // some test here
}

Once I did that all my coverage began working and the issues were resolved.

-2

I'm using eclemma 2.3.2 and it's working perfectly on eclipse

I only need to add these dependencies in my pom.xml

<dependency>
    <groupId>org.jboss.arquillian.extension</groupId>
    <artifactId>arquillian-jacoco</artifactId>
    <version>1.0.0.Alpha6</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.jacoco</groupId>
    <artifactId>org.jacoco.core</artifactId>
    <version>0.7.1.201405082137</version>
    <scope>test</scope>
</dependency>

Then I build the project, update maven projects configuration and run coverage plugin as expected

Kurohige
  • 333
  • 2
  • 10