Cobertura:读取文件时出错cobertura.ser:null java.io.EOFException:null

时间:2016-12-20 14:26:51

标签: mockito cobertura maven-cobertura-plugin

我已经在Cobertura工作了几个月,最近我收到一条错误,上面写着"错误阅读.ser文件"从特定的目标位置。有谁知道为什么我得到以下错误:

Cobertura maven插件版本:2.7

Java版本:1.8

    [INFO] Cobertura 2.1.1 - GNU GPL License (NO WARRANTY) - See COPYRIGHT file
[ERROR] Cobertura: Error reading file C:\view\
xyz\target\cobertura\cobertura.s
er: null
java.io.EOFException: null
        at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputSt
ream.java:2626) ~[na:1.8.0_102]
        at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1321) ~[
na:1.8.0_102]
        at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:20
18) ~[na:1.8.0_102]
        at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1942)
 ~[na:1.8.0_102]
        at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1
808) ~[na:1.8.0_102]
        at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1353) ~[
na:1.8.0_102]
        at java.io.ObjectInputStream.readObject(ObjectInputStream.java:373) ~[na
:1.8.0_102]
        at java.util.HashMap.readObject(HashMap.java:1404) ~[na:1.8.0_102]
        at sun.reflect.GeneratedMethodAccessor3.invoke(Unknown Source) ~[na:na]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:43) ~[na:1.8.0_102]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_102]
        at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:105
8) ~[na:1.8.0_102]
        at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1909)
 ~[na:1.8.0_102]
        at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1
808) ~[na:1.8.0_102]
        at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1353) ~[
na:1.8.0_102]
        at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:20
18) ~[na:1.8.0_102]
        at java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:50
3) ~[na:1.8.0_102]
        at net.sourceforge.cobertura.coveragedata.CoverageDataContainer.readObje
ct(CoverageDataContainer.java:321) ~[cobertura-2.1.1.jar:2.1.1]
        at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source) ~[na:na]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:43) ~[na:1.8.0_102]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_102]
        at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:105
8) ~[na:1.8.0_102]
        at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1909)
 ~[na:1.8.0_102]
        at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1
808) ~[na:1.8.0_102]
        at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1353) ~[
na:1.8.0_102]
        at java.io.ObjectInputStream.readObject(ObjectInputStream.java:373) ~[na
:1.8.0_102]
        at java.util.HashMap.readObject(HashMap.java:1404) ~[na:1.8.0_102]
        at sun.reflect.GeneratedMethodAccessor3.invoke(Unknown Source) ~[na:na]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:43) ~[na:1.8.0_102]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_102]
        at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:105
8) ~[na:1.8.0_102]
        at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1909)
 ~[na:1.8.0_102]
        at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1
808) ~[na:1.8.0_102]
        at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1353) ~[
na:1.8.0_102]
        at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:20
18) ~[na:1.8.0_102]
        at java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:50
3) ~[na:1.8.0_102]
        at net.sourceforge.cobertura.coveragedata.CoverageDataContainer.readObje
ct(CoverageDataContainer.java:321) ~[cobertura-2.1.1.jar:2.1.1]
        at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source) ~[na:na]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:43) ~[na:1.8.0_102]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_102]
        at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:105
8) ~[na:1.8.0_102]
        at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1909)
 ~[na:1.8.0_102]
        at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1
808) ~[na:1.8.0_102]
        at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1353) ~[
na:1.8.0_102]
        at java.io.ObjectInputStream.readObject(ObjectInputStream.java:373) ~[na
:1.8.0_102]
        at java.util.HashMap.readObject(HashMap.java:1404) ~[na:1.8.0_102]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.
0_102]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:62) ~[na:1.8.0_102]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:43) ~[na:1.8.0_102]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_102]
        at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:105
8) ~[na:1.8.0_102]
        at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1909)
 ~[na:1.8.0_102]
        at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1
808) ~[na:1.8.0_102]
        at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1353) ~[
na:1.8.0_102]
        at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:20
18) ~[na:1.8.0_102]
        at java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:50
3) ~[na:1.8.0_102]
        at net.sourceforge.cobertura.coveragedata.CoverageDataContainer.readObje
ct(CoverageDataContainer.java:321) ~[cobertura-2.1.1.jar:2.1.1]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.
0_102]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:62) ~[na:1.8.0_102]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:43) ~[na:1.8.0_102]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_102]
        at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:105
8) ~[na:1.8.0_102]
        at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1909)
 ~[na:1.8.0_102]
        at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1
808) ~[na:1.8.0_102]
        at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1353) ~[
na:1.8.0_102]
        at java.io.ObjectInputStream.readObject(ObjectInputStream.java:373) ~[na
:1.8.0_102]
        at net.sourceforge.cobertura.coveragedata.CoverageDataFileHandler.loadCo
verageData(CoverageDataFileHandler.java:85) ~[cobertura-2.1.1.jar:2.1.1]
        at net.sourceforge.cobertura.coveragedata.CoverageDataFileHandler.loadCo
verageData(CoverageDataFileHandler.java:62) ~[cobertura-2.1.1.jar:2.1.1]
        at net.sourceforge.cobertura.dsl.Cobertura.getProjectDataInstance(Cobert
ura.java:146) [cobertura-2.1.1.jar:2.1.1]
        at net.sourceforge.cobertura.dsl.Cobertura.report(Cobertura.java:122) [c
obertura-2.1.1.jar:2.1.1]
        at net.sourceforge.cobertura.reporting.ReportMain.parseArgumentsAndRepor
t(ReportMain.java:91) [cobertura-2.1.1.jar:2.1.1]
        at net.sourceforge.cobertura.reporting.ReportMain.generateReport(ReportM
ain.java:141) [cobertura-2.1.1.jar:2.1.1]
        at net.sourceforge.cobertura.reporting.ReportMain.main(ReportMain.java:1
51) [cobertura-2.1.1.jar:2.1.1]
Report time: 1031ms

提前致谢。

3 个答案:

答案 0 :(得分:1)

通常.ser文件只有在没有足够的内存分配给cobertura生成报告时才会被锁定。解决方案应该是

1)从src / test / java中删除所有不必要的测试类文件,然后运行cobertura或

2)增加cobertura的内存大小,如下所示

    <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>cobertura-maven-plugin</artifactId>
            <version>2.7</version>
            <configuration>
                <quiet>true</quiet>
                <cobertura.maxmem>1024m</cobertura.maxmem>
                <instrumentation>
                    <excludes>
                        <exclude>com/homepage/helper/ABC.class</exclude>

                    </excludes>
                    <ignores>
                        <!-- <ignore>com..homepage..helper.ABC</ignore> -->
                    </ignores>
                    <maxmem>1024m</maxmem>
                </instrumentation>
                <argLine>-Xms512m -Xmx2048m -XX:MaxPermSize=1024m</argLine>
                <!-- <argLine>-Xmx2048m</argLine> -->
            </configuration>
            <executions>
                <execution>
                    <id>clean</id>
                    <phase>pre-site</phase>
                    <goals>
                        <goal>clean</goal>
                    </goals>
                </execution>
                <execution>
                    <id>instrument</id>
                    <phase>site</phase>
                    <goals>
                        <goal>instrument</goal>
                        <goal>cobertura</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

在pom.xml中,只需将 cobertura.maxmem 大小更改为我们想要的任何内容,我们也可以选择忽略几个类同时生成报告。

3)还有一种可能性,你的sure-fire插件版本不受支持。如果以上两个解决方案无效,请尝试更新如下所示的确定版本( maven-surefire-plugin 应为2.12):

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.12</version>
            <dependencies>
                <dependency>
                    <groupId>org.apache.maven.surefire</groupId>
                    <artifactId>surefire-junit47</artifactId>
                    <version>2.17</version>
                </dependency>
            </dependencies>             
            <configuration>                     
                     <groups>${testcase.include}</groups>
                     <excludedGroups>${testcase.exclude}</excludedGroups>
            </configuration>

        </plugin>

希望它有用。

答案 1 :(得分:1)

尝试按照上述更新内存,这对我们不起作用。但是,通过另一种方式无意中找到了“解决方案”(或至少补救措施):

我们有大约60个用类级别注释

修饰的测试类
@RunWith(PowerMockRunner.class)

巧合的是,由于这些类没有在我们的本地环境(实际上使用EclEmma进行代码覆盖 - 与JaCoCo - KNOWN PROBLEM相同)中显示覆盖,我尝试转换注释...

@RunWith(PowerMockRunner.class)

@RunWith(MockitoJUnitRunner.class)

并且能够转换我们所拥有的60个中的大约30个。虽然我们无法转换所有这些,但由于某些功能与测试中的PowerMock功能相关,例如允许测试静态方法。但是将注释从PowerMockRunner转换为MockitoJUnitRunner仍然以某种方式弥补了上述错误导致Cobertura错误的问题。我仍然无法给出实质性答案,因为Cobertura确实用@RunWith(PowerMockRunner.class)显示了类的覆盖范围。但是一旦我知道,我会更新这篇文章。

答案 2 :(得分:0)

检查文件旁边是否也有cobertura.ser.lock文件。如果存在,则问题是在虚拟机关闭时,cobertura在运行结束时写出覆盖率数据。根据您上过多少课,可能需要一段时间。如果Surefire插件默认情况下在30秒内未关闭,则有时会在过程中使文件处于锁定状态,从而杀死VM。您可以使用surefire 2.20或更高版本并添加更大的关机超时值(forkedProcessExitTimeoutInSeconds)来解决该问题。这是一个示例:

 <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.20</version>
    <configuration>
         <forkedProcessExitTimeoutInSeconds>120</forkedProcessExitTimeoutInSeconds>
    </configuration>
 </plugin>