3

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

Mike K.
  • 543
  • 3
  • 14
  • 46
  • 1
    "Are my tests maybe not right" You could understand that if the coverage was slightly different (different tools count different things as having been "covered"). But not when the difference is 100% – Michael Jul 24 '20 at 21:26
  • 1
    Wait. You contradict yourself, don't you? "mine is showing some coverage" "Sonarqube it is showing 0%" – Michael Jul 24 '20 at 21:27
  • @Michael Sonarqube is showing 45% code coverage for the entire app. On this one specific file, it is showing 0%. I don't know why it is showing 0%, when my intellij is showing it as 100% coverage. – Mike K. Jul 24 '20 at 21:31
  • 1
    Have you omitted something? Because the test you have shown does not cover the 100% of the class, so it is strange that IntelliJ would give that result. The 3 param constructor, for example, is not covered. I'm wondering whether you are running some additional test harness (e.g. integration test) in IntellIJ which what's actually giving you the coverage there, while the same harness is not running in Sonar. – Michael Jul 24 '20 at 21:33
  • 1
    And what test harness are you using (edit the details into the question)? TestNG? JUnit? JUnit requires any @Test methods to be public (not sure about the class, might need to be public too). So that might be one reason the test isn't being run – Michael Jul 24 '20 at 21:35
  • @Michael I'm using JUnit. I added "public" to the class & methods, but didn't help. I then added a simple test (adding 2 numbers together), and it didn't catch it. I think the problem is that when I generated these tests, that I did it with IntelliJ that auto generated a test file. That doesn't add public and maybe other things. I say that bc I went to a file that has successful coverage on it, & I removed "public" from the class and method, and the build fails saying "The class ... is not public --- and .. class should have exactly one public constructor --- and: method ... should be public" – Mike K. Jul 25 '20 at 02:44

1 Answers1

4

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.
Adam Wise
  • 2,043
  • 20
  • 17