Question

I have written JUnit tests for my class, and would like it to tell me if there is any part of my code that is not unit tested. Is there a way to do this?

Was it helpful?

Solution

If you use Eclipse, you can also try EclEmma, which shows you which lines of source were covered by your test. This is sometimes more useful than running a coverage tool like Cobertura because you can run a single test from inside Eclipse and then get immediate feedback on what was covered.

OTHER TIPS

Yes, coverage tools like cobertura or emma.

They create reports that show every line in the source code and whether it was executed or not (and aggregated statistics as well).

Of course, they can only show you if the code was run. There is no way to tell if the unit test contained assertions to confirm that the result was correct.

You need some code coverage tools. See here (http://java-source.net/open-source/code-coverage) for some

If you look at the first one I think it does what you need

Cobertura is a free Java tool that calculates the percentage of code accessed by tests. It can be used to identify which parts of your Java program are lacking test coverage. It is based on jcoverage. Features of Cobertura:

  • Can be executed from ant or from the command line.

Your headline and your actual question differ. The tools mentioned in the other answers can tell you, which part of the code were not tested (=not executed at all). Making "make sure that all parts of code is unit tested" is a different thing. The coverage tools can tell you whether all lines/instructions have been executed, but they don't guarantee that everything is tested functionally (all constellations of data, all execution paths, etc.). This requires some brain power. In my opinion, test coverage often gives a wrong feeling of safety. E.g. testing trivial getters increases coverage a lot but is rather useless.

If you are using IntelliJ then there is a button titled

"Run With Coverage"

Run with Coverage

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top