tags:

views:

94

answers:

4

Is there a tool for Java which, given a set of JUnit tests, and a class to test, will tell you which lines of the class are tested by the tests? ie. required to be present for the tests to run successfully. I don't mean "code coverage", which only tells you whether a line is executed, but something stronger than that: Is the line required for the test to pass?

I often comment out a line of code and run a test to see if the test really is testing that line of code. I reckon this could be done automatically by a semi-smart tool (eg. something like an IDE that can work out what can be removed from a method whilst keeping it compilable).

+1  A: 

I use emma for most of my projects. i included it in my ant build file and it generates html files for the reports

two other coverage projects i read about but haven't tried yet are clover or cobertura

Nikolaus Gradwohl
+2  A: 

I love cobertura, because the generated reports are IMHO the most beautiful. And it has its own ant target!

In comparison to emma, it has also branch coverage, not only line coverage, which is misleading very often.

Daniel
+1 IMO cobertura is the best solution to this problem
bwawok
Cobertura is great for code coverage, but I think the OP is looking for assertion coverage. E.g. you can have 100% code coverage, but assert nothing. The OP is looking for a tool to help identify where assertions are lacking.
Chris Knight
+3  A: 

There's an open source mutation-testing tool called Jester that changes the lines of your source code, then runs your tests, and reports whether your tests passed anyway. Sounds closer to what you're looking for than code coverage tools.

Jester is a test tester for testing your java JUnit tests (Pester is for Python PyUnit tests). It modifies your source code, runs the tests and reports if the tests pass despite the changes to the code. This can indicate missing tests or redundant code.

WRT the discussion about whether these tools are needed in a pure TDD project, there is a link on the Jester project webpage to a posting about the benefits of using Jester on code written during a TDD session (Uncle Bob's infamous bowling TDD example).

Nathan Hughes
+2  A: 

What you are looking for might be referred to as mutation testing. While mutation testing won't tell you which lines of code are required to pass, per se. What mutation testing does is modify your source code looking for changes it can make to your code but your test still passes. E.g. changing

if (a < b)

to

if (a >= b)

and seeing if the test still passes. This will highlight weaknesses in your test.

Another java library for mutation testing is jumble.

Chris Knight