简体   繁体   English

Maven Cobertura插件生成不完整的覆盖率报告

[英]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). 这实际上没有在pom.xml文件中配置,因此它仅使用插件的最新版本(当前为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. 总体而言,它仅报告约1%的覆盖率,但这实际上是一个相当重要的类,我知道它已经被大量使用。 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. 第一行显然称为2053年时间。 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. 第二行实际上没有运行任何代码,因此留为空白(如所示),但是第三行和第四行均报告被调用0次。

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. 同样,第一行被调用2053次,但是第二行(以及构造函数中的所有其他行)被调用0次。

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). 源文件本身的重量很长,为2040行(918条实际代码行计入覆盖范围)。

I've been writing other tests and code over the last few days and cobertura is working fine for those. 在过去的几天中,我一直在编写其他测试和代码,而cobertura对此工作正常。

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). 现在该类的长度为804行(就可以覆盖的行而言)。

So it looks like there is some sort of effective limit on how large a class cobertura can handle. 因此,对于cobertura类可以处理的大小,似乎存在某种有效的限制。

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. 更新(2015年6月3日):我将相关类重构到790行,但仍然存在相同的问题。 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. 这样,我知道不再需要构造函数内部的try / catch了。

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. 我不是100%确定这是否通过缩短构造函数的长度来解决问题,或者是否通过移动try / catch来解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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