IntelliJ IDEA: ignore trivial methods in code cove

2019-06-15 10:04发布

问题:

In IntelliJ IDEA 15.0.2 how can I ignore trivial getters and setters (trivial methods) during test coverage measurement?

// should be measure
public void complex() {
    fancy();
    interesting();
    dropDatabase();
}

// should not be measured
public int getNumber() {
    return this.number;
}

Measuring every line would result in 75%. Measuring only the above method would result in 100%. And those are 100% of the code useful for testing.

How come I don't find anything about that on the Internet? Am I diving into bad practice?


UPDATE

This code is also eligible for testing:

// should also be tested as it contains logic
public Integer getValidationProgress() {
    if (validationProgress == null) {
        validationProgress = 0;
    }
    return validationProgress;
}

回答1:

JetBrains told me that this is currently not possible.

Andrey Dernov (IntelliJ) Jan 6, 22:54

Hello Michael,

There is no setting to ignore a certain method.

I created an issue for that.



回答2:

an even simpler example:

public abstract class A {
    public static int add(int x, int y) {
        return x + y;
    }
}

Here IntelliJ's coverage complains about a not-tested constructor of A. I'd have to write something stupid like

new A() {};

into my test to get it tested. If I use this approach for a helper class

public final class A {
    private A() {}

    public static int add(int x, int y) {
        return x + y;
    }
}

I need to use reflection to "test" the empty code:

final Class<?> clazz = Class.forName("package.name.of.A");
final Constructor<?> constructor = clazz.getDeclaredConstructors()[0];

constructor.setAccessible(true);
constructor.newInstance();

which does not look much smarter.