简体   繁体   English

JaCoCo 在持续测试 Comparable 时需要忽略 Lombok 生成的代码 - 怎么办?

[英]Need to ignore Lombok Generated Code for JaCoCo while it keeps testing Comparable - How to do?

There are many articles here about that instructing to use lombok.config file inside your project root and add these entries in it:这里有很多关于指示在项目根目录中使用lombok.config文件并在其中添加这些条目的文章:

lombok.addLombokGeneratedAnnotation = true

But my issue is slightly different.但我的问题略有不同。 The class that has Lombok annotations also implements Comparable and then the method compareTo.具有 Lombok 注释的 class 也实现了 Comparable,然后实现了方法 compareTo。

@Document(collection = "item")
@Data
@Builder
public class Item implements Serializable, Comparable<Item> {

    @JsonIgnore
    private static final long serialVersionUID = 5816882082047405354L;
    @Id
    @JsonProperty
    private String id;
    @JsonProperty
    private String nome;
    @JsonProperty
    private Double valor;

    public int compareTo(Item o) {
        int retorno = this.nome.compareToIgnoreCase(o.getNome());
        if (retorno == 0) {
            if (this.getValor() < o.getValor()) {
                retorno = -1;
            } else if (this.getValor() > o.getValor()) {
                retorno = 1;
            }
        }
        return retorno;
    }
}

And this is my test class.这是我的测试 class。

public class TestItem {

    @Test
    public void shouldCompareFullAndBeEqual() {
        final Item item1 = Item.builder().nome("Item").valor(20.0).build();
        item1.setId("IDDCLIP");
        final Item item2 = Item.builder().nome("Item").valor(20.0).build();
        item2.setId("IDDCLIP");
        assertEquals(item1.compareTo(item2), item2.compareTo(item1));
    }    

    @Test
    public void shouldCompareAndBeDifferentByValue() {
        final Item item1 = Item.builder().nome("Item").valor(30.0).build();
        item1.setId("IDDCLIP");
        final Item item2 = Item.builder().nome("Item").valor(20.0).build();
        item2.setId("IDDCLIP");
        assertNotEquals(item1.compareTo(item2), item2.compareTo(item1));
    }

    @Test
    public void shouldCompareAndBeDifferentByVName() {
        final Item item1 = Item.builder().nome("Item1").valor(20.0).build();
        item1.setId("IDDCLIP");
        final Item item2 = Item.builder().nome("Item").valor(20.0).build();
        item2.setId("IDDCLIP");
        assertNotEquals(item1.compareTo(item2), item2.compareTo(item1));
    }

    @Test
    public void shouldCompareAndBeDifferentByNameAndValue() {
        final Item item1 = Item.builder().nome("Item1").valor(30.0).build();
        item1.setId("IDDCLIP");
        final Item item2 = Item.builder().nome("Item").valor(20.0).build();
        item2.setId("IDDCLIP");
        assertNotEquals(item1.compareTo(item2), item2.compareTo(item1));
    }    
}

When I run my Unit Tests over this class although it tests the compareTo() it does not appears on my code coverage report as covered.当我在此 class 上运行单元测试时,尽管它测试了compareTo() ,但它并没有出现在我的代码覆盖率报告中。

我的报道 Can anyone help me?谁能帮我?

  1. I notice that your screenshot contains @EqualsAndHashCode.Exclude , while your code does not.我注意到您的屏幕截图包含@EqualsAndHashCode.Exclude ,而您的代码没有。 Who knows what other things are different in your setup?谁知道您的设置中还有哪些不同之处?
  2. The @Generated annotation, and hence your lombok.config file, should have no influence on the details of coverage. @Generated注释以及您的lombok.config文件应该不会影响覆盖的细节。 I would expect either no coverage data at all or full.我希望根本没有覆盖数据或完整。 Unfortunately, you to not tell us how exactly you measure your coverage.不幸的是,您不能告诉我们您如何准确衡量您的覆盖范围。
  3. The class itself is fine and (almost) fully covered by your tests. class 本身很好,并且(几乎)完全被您的测试覆盖。 I confirmed that manually.我手动确认了。 However, as previously stated, my setup may be quite different than yours.但是,如前所述,我的设置可能与您的设置完全不同。

I managed to solve the problem.我设法解决了这个问题。 The @Test annotation was wrong. @Test 注释是错误的。 I was using a wrong package.我使用了错误的 package。 After that and a mvn clean the coverage became 100%之后,mvn clean 覆盖率变为 100%

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

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