简体   繁体   中英

Maven Cobertura plugin generating incomplete coverage report

I'm using:

mvn cobertura:cobertura

to generate a coverage report for a project. This isn't actually configured in the pom.xml file, so it's just using the latest version of the plugin (currently 2.6).

For the most part this works ok, but for some reason one class has a very odd report. It seems to be reporting that some lines have been covered, but other lines (which are right next to it) are not.

I've been running:

mvn clean

Of course, but that doesn't seem to help.

Overall it's only reporting about 1% coverage, but this is actually quite a key class that I know is getting used a lot. I also know that the reports used to work ok. I'm not sure when they stopped working, as I've not inspected that class's coverage for a while.

As an example of what I'm seeing:

2053 private final List<EntityProperty<T>> properties = new ArrayList<EntityProperty<T>>();     
     private final PropertyDescriptor idProperty;
0    private final Set<String> fieldOrder = new LinkedHashSet<String>();
0    private final Map<String, String> additionalFieldLabels = new HashMap<String, String>();

This is from the initialiser of the class. The first line apparently gets called 2053 time. The 2nd line doesn't actually run any code, so is left blank (as indicated), but the 3rd and 4th lines both report being called 0 times.

Another example:

2053 public EntityIntrospector(AdminApp app, EntityApplication entityApp, Class<T> entityClass) {
0        this.app = app;

From the constructor itself. Again the first line is called 2053 times, but the 2nd (and all other lines in the constructor) are called 0 times.

Anyway I'm at a loss as to why this is happening.

I suspect that maybe it's another library somehow interfering with the coverage/instrumentation somehow.

Possibly the class size could be a factor. The source file itself is a pretty weighty 2040 lines long (918 actual lines of code that count towards coverage).

I've been writing other tests and code over the last few days and cobertura is working fine for those.

Hints and suggestions welcome.

So it does seem to seem the class was too large. I hacked at the class in question to comment out some lines. This obviously made a whole load of tests fail, but coverage now seems to be working properly again.

The class is now 804 lines long (in terms of lines that can be covered).

So it looks like there is some sort of effective limit on how large a class cobertura can handle.

In my case this probably means that class could do with refactoring to break the code up a bit.

UPDATE (3rd June 2015): I refactored the class in question down to 790 lines and still had the same problem. In the end the issue seemed to be related to the code in the constructor. I had something like:

try {
    final BeanInfo beanInfo = Introspector.getBeanInfo(entityClass);
    // rest of constructor here (about 50 lines)
}
catch( IntrospectionException ie ) {
    throw new RuntimeException(ie);
}

I changed that to have a method like:

protected BeanInfo getBeanInfo(Class<T> entityClass) {
    try {
        return Introspector.getBeanInfo(entityClass);
    }
    catch( IntrospectionException ie ) {
        throw new RuntimeException(ie);
    }
}

Which was instead called from within the constructor. That way I know longer needed the try/catch inside the constructor itself.

I'm not 100% sure if this fixed the issue by making the constructor shorter in length or if moving the try/catch made the difference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM