简体   繁体   中英

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.addLombokGeneratedAnnotation = true

But my issue is slightly different. The class that has Lombok annotations also implements Comparable and then the method 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.

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.

我的报道 Can anyone help me?

  1. I notice that your screenshot contains @EqualsAndHashCode.Exclude , while your code does not. 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. 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. 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. I was using a wrong package. After that and a mvn clean the coverage became 100%

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