Mike K.
Mike K.

Reputation: 606

Sonarqube only showing some test coverage for Java tests

My java app has 45% code coverage on it right now. I've been adding new tests and they have been getting scanned both by my app (through mvn test and with Intellij showing the coverage %) and through Sonarqube.

here is my test file:

public class BadRequestAlertExceptionTest {

    BadRequestAlertException baExc = new BadRequestAlertException("testDefaultMessage","testEntityName");
    @Test
    public void getEntityName() {
        assertEquals("testEntityName", baExc.getEntityName());
    }
}

I am trying to add code coverage to the following file:

public class BadRequestAlertException extends AbstractThrowableProblem {
    private static final long serialVersionUID = 1L;
    private final String entityName;
    public BadRequestAlertException(String defaultMessage, String entityName) {
        this(ErrorConstants.DEFAULT_TYPE, defaultMessage, entityName);
    }
    public BadRequestAlertException(URI type, String defaultMessage, String entityName) {
        super(type, defaultMessage, Status.BAD_REQUEST, null, null, null, getAlertParameters(entityName));
        this.entityName = entityName;
    }
    public String getEntityName() {
        return entityName;
    }
    private static Map<String, Object> getAlertParameters(String entityName) {
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("message", "error.");
        parameters.put("params", entityName);
        return parameters;
    }
}

Locally I can see that the file is at 100% code coverage, but on Sonarqube it is showing 0%. Anyone know why? I know Sonarqube is setup correctly as it has been increasing the coverage % for my other files just fine.

The test harness I am using is JUnit.

Are my tests maybe not right, so Sonarqube doesn't accept it as being 100%, while my 'mvn test' and my IDE is accepting it as 100%?

And I looked at SonarQube not picking up Unit Test Coverage, but that question is more for if Sonarqube is showing 0% coverage (mine is showing some coverage)

This shows intellij having it as 100% coverage

This shows sonarqube having this single file at 0% coverage

Upvotes: 3

Views: 2785

Answers (1)

Adam Wise
Adam Wise

Reputation: 2290

In my recent experience, it is likely you are not importing the right test package. Two things that are supposed to make Java development easier are working against you.

  • First, the IDE will often auto-suggest an import when you annotate a class with '@Test'. However, 'Test' is a common namespace/classname, so the auto-suggestion may be for a local class, and not org.junit.test.
  • Second, Java editors can collapse the import section entirely, making it harder to catch that issue.

Upvotes: 4

Related Questions