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"));
}
}
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.
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.
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.