0

I have the following class using Saxon S9 API, whose only purpose is to run an XQuery (.xq) file.

public class SaxonXQuery {
  public void executeXQuery(String xQueryFilename) throws IOException, SaxonApiException {
    try {
      Processor saxon = new Processor(false);
      XQueryCompiler compiler = saxon.newXQueryCompiler();
      XQueryExecutable exec = compiler.compile(new File(String.valueOf(getPath(xQueryFilename))));
      XQueryEvaluator query = exec.load();
      StringWriter sw = new StringWriter();
      query.run(saxon.newSerializer(sw));
      String result = sw.toString();
      log.info("XQuery result: {} ", result);
      PrintWriter out =
          new PrintWriter(
              new File(String.valueOf(getPath("testResources")))
                  + "/results/"
                  + xQueryFilename
                  + "Result.xml",
              StandardCharsets.UTF_8);
      out.println(result);
      out.close();
    } catch (Exception e) {
      log.debug("Error while executing XQuery: {}", e.getMessage());
      throw e;
    }
  }
}

Unfortunately, I have 0% code coverage coming from Jacoco, even though I have the following test class which should cover all lines:

public class SaxonXQueryTest {
  private Path result;
  private Path expected;
  private final SaxonXQuery xQueryManager = new SaxonXQuery();

  @BeforeEach
  void setUp() {
    result = null;
    expected = null;
  }

  @Test
  public void executeXQueryTest_queryForId() throws IOException, SaxonApiException {
    xQueryManager.executeXQuery("returnId");
    expected = getPath("expectedReturnId");
    result = getPath("returnIdResult");
    Object[] resultValue;
    try (Stream<String> s = Files.lines(result)) {
      resultValue = s.toArray();
    }
    try (Stream<String> s = Files.lines(expected)) {
      assertThat(s.toArray(), Is.is(resultValue));
    }
  }

  @Test
  public void executeXQueryTest_queryForPriority() throws IOException, SaxonApiException {
    xQueryManager.executeXQuery("returnPriority");
    result = getPath("returnPriorityResult");
    Stream<String> s = Files.lines(result);
    assertNotNull(s.toString());
    s.close();
  }

  @Test
  public void executeXQueryTest_queryForValue() throws IOException, SaxonApiException {
    xQueryManager.executeXQuery("returnValue");
    expected = getPath("expectedReturnValue");
    result = getPath("returnValueResult");
    Object[] resultValue;
    try (Stream<String> s = Files.lines(result)) {
      resultValue = s.toArray();
    }
    try (Stream<String> s = Files.lines(expected)) {
      assertThat(s.toArray(), Is.is(resultValue));
    }
  }

  @Test
  public void executeXQueryTest_whenFailed_shouldThrow() {
    assertThrows(Exception.class, () -> xQueryManager.executeXQuery("wagabooga"));
  }
}

Jacoco coverage status

I've tried changing the Exceptions, changing try{} branch arrangements, even removing the try/catch, and for some reason, it still doesn't detect this class as tested in the code.

IntelliJ says I have 100% coverage. But I need to understand what's going on with Jacoco, and what could possibly cause it to miss/not detect these tests? They are running, btw.

1 Answers1

0

Not included in the code was my imports list, which eventually led me to the solution:

import org.junit.Test;

should've been

import org.junit.jupiter.api.Test;

Though both are JUnit imports, Jacoco only considers one of these when calculating coverage.

  • Statement "Jcoco only considers one of these" is untrue - JaCoCo is independent of the testing framework, it works even if you run your code manually without testing framework. Most likely change of import helps in your case because your execution environment (Maven?) understand only how to run JUnit 4 - for example maybe you simply forgot to update maven-surefire-plugin like https://github.com/jacoco/jacoco/issues/1048 – Godin Apr 18 '22 at 21:15
  • @Godin Sorry, I assumed it was that way because it was a logical explanation for what was happening. And that thread is super enlightening. Thank you. – Tiberius Dourado Apr 19 '22 at 16:00