2011-11-22 77 views
3

我有maven 3,cobertura maven插件2.51和一些classe。 我需要知道我的课程的测试覆盖率。但我不想测试setter/getters。所以我只是想忽略它们而已。忽略课堂中的方法。 cobertura maven插件

<plugin> 
       <groupId>org.codehaus.mojo</groupId> 
       <artifactId>cobertura-maven-plugin</artifactId> 
       <version>2.5.1</version> 
       <configuration> 
        <formats> 
         <format>html</format> 
         <format>xml</format> 
        </formats> 
        <check> 
         <haltOnFailure>false</haltOnFailure> 
         <lineRate>55</lineRate> 
         <branchRate>60</branchRate> 
         <packageLineRate>60</packageLineRate> 
         <packageBranchRate>60</packageBranchRate> 
         <totalLineRate>60</totalLineRate> 
         <totalBranchRate>60</totalBranchRate> 
        </check> 
        <instrumentation> 
         <excludes> 
          <exclude>com/FileCopier*.*</exclude> 
          <exclude>com/FileCopierWithCamel*.*</exclude> 
          <exclude>com/Main*.*</exclude> 
         </excludes> 
        </instrumentation> 
       </configuration> 
       <executions> 
        <execution> 
         <goals> 
          <goal>clean</goal> 
          <goal>cobertura</goal> 
          <goal>check</goal> 
         </goals> 
         <phase>package</phase> 
        </execution> 
       </executions> 
      </plugin> 

然后,添加以下忽略阻止

<ignores> 
          <!-- Ignore all setter and getter methods in your classes --> 
          <ignore>com.*.set*</ignore> 
          <ignore>com.*.get*</ignore> 
          <ignore>com.MyClass.getName</ignore> 
         </ignores> 

但看起来这是行不通的。

我发现此链接: http://jira.codehaus.org/browse/MCOBERTURA-52 看起来像这个问题是5岁左右。 有没有我的问题的解决方案?

回答

0

我不知道如何忽略方法,但是您可以使用一种工具为您的getter和setter自动生成单元测试,以便涵盖这些方法。我不知道这是否解决了你的确切问题,因为现在的覆盖率低于预期,你的覆盖率会高于预期,但似乎总比没有好。

有大约正是在这里,这一个SO问题:Is there a Java unit-test framework that auto-tests getters and setters?

6

如果只有约getter和setter,你可以设置ignoreTrival开关:

Cobertura Changelog - --ignoreTrivial开关,告诉Cobertura忽略覆盖率报告中的以下 :Getter方法,只需 读取类字段;设置类字段的Setter方法; 仅设置类字段并调用超类 类构造函数的构造方法。

来源:Is there still no solution for ignoring setter/getter (other trivial methods) with the cobertura-maven-plugin?

如果您想更具体忽略方法,你也可以使用ignoreMethodAnnotation开关:

Cobertura Changelog - 用于指定注释--ignoreMethodAnnotation交换机, 如果出现在方法中,将导致Cobertura忽略覆盖率报告中的 方法。

文档:Documentation of ignoreMethodAnnotation(他们做了一个小错误:它们定义了CoberturaIgnore注解,但是随后他们使用CoverageIgnore

例如,在pom.xml

<plugin> 
    <groupId>org.codehaus.mojo</groupId> 
    <artifactId>cobertura-maven-plugin</artifactId> 
    <version>2.7</version> 
    <configuration> 
    <instrumentation> 
     <ignoreTrivial>true</ignoreTrivial> 
     <ignoreMethodAnnotation>foo.bar.CoberturaIgnore</ignoreMethodAnnotation> 
    </instrumentation> 
    </configuration> 
</plugin> 
0

我们正在使用的Cobertura为春季 - 启动Restful API单元测试代码覆盖度量标准。

我们有类似的情况为了忽略单元测试的代码覆盖度量报告中的一些类,使用hibernate本机查询的类不能用JPATest类进行单元测试(如在内存数据库中)。出于任何原因,如果你需要的Cobertura忽略一些你的类从这些指标然后执行以下操作...

的pom.xml

<build> 
     <plugin> 
       <groupId>org.codehaus.mojo</groupId> 
       <artifactId>cobertura-maven-plugin</artifactId> 
       <version>2.7</version> 
       <configuration> 
        <formats> 
         <format>html</format> 
         <format>xml</format> 
        </formats> 
        <instrumentation> 
         <ignoreTrivial>true</ignoreTrivial> 
         <ignoreMethodAnnotation>com.thermofisher.micro.common.annotation.CoberturaIgnore</ignoreMethodAnnotation>   
        </instrumentation> 
       </configuration> 
       <executions> 
        <execution> 
        <goals> 
         <goal>clean</goal> 
        </goals> 
        </execution> 
       </executions> 
      </plugin> 
     </plugins> 
    </build> 
    <reporting> 
     <plugins> 
      <plugin> 
       <groupId>org.codehaus.mojo</groupId> 
       <artifactId>cobertura-maven-plugin</artifactId> 
       <version>2.7</version> 
       <configuration> 
        <formats> 
         <format>html</format> 
         <format>xml</format> 
        </formats> 
        <instrumentation> 
         <ignoreTrivial>true</ignoreTrivial> 
         <ignoreMethodAnnotation>com.thermofisher.micro.common.annotation.CoberturaIgnore</ignoreMethodAnnotation>   
        </instrumentation> 
       </configuration> 
      </plugin> 
     </plugins> 
    </reporting> 

注释

package com.any.package.annotation; 

import java.lang.annotation.Documented; 
import java.lang.annotation.ElementType; 
import java.lang.annotation.Retention; 
import java.lang.annotation.RetentionPolicy; 
import java.lang.annotation.Target; 

@Retention(RetentionPolicy.RUNTIME) 
@Documented 
@Target({ElementType.METHOD, ElementType.TYPE, ElementType.PACKAGE}) 
public @interface CoberturaIgnore {} 

把注释使用:

package com.any.package.repository; 

import com.any.package.annotation.CoberturaIgnore; 

@CoberturaIgnore 
@Repository 
public class SequenceIdRespositoryImpl implements SequenceIdRespository { 

    @PersistenceContext 
    private EntityManager entityManager; 


    @CoberturaIgnore 
    @Override 
    public long getUserContactKey() { 
     Query query = entityManager.createNativeQuery("select user_key_seq.nextval from dual"); 
     return getKey(query); 
    } 
}