通过gradle写xlsx

时间:2014-01-14 09:16:30

标签: java gradle apache-poi xlsx

我用硒写测试。我写了一个testrunner,将测试结果写入xlsx文件。 问题是,这只适用于日食! 如果我通过'gradle'运行2个或更多个测试类,那么只有第一个类执行testprotocol,才能在第一个类抛出异常后修改每个类。结果是,只有第一个测试类结果被写入协议。

de.h2o.testing.testprotocol.Screenshot STANDARD_ERROR
    Jan 14, 2014 9:49:21 AM de.h2o.testing.testprotocol.TestProtocol writeToFile
    Warnung: error occured while writing protocol file: org.apache.xmlbeans.impl.values.XmlValueDisconnectedException: null
org.apache.xmlbeans.impl.values.XmlValueDisconnectedException
    at org.apache.xmlbeans.impl.values.XmlObjectBase.check_orphaned(XmlObjectBase.java:1247)
    at org.apache.xmlbeans.impl.values.XmlObjectBase.setterHelper(XmlObjectBase.java:1980)
    at org.apache.xmlbeans.impl.values.XmlObjectBase.set(XmlObjectBase.java:2062)
    at org.apache.poi.xssf.usermodel.XSSFRow.createCell(XSSFRow.java:170)
    at org.apache.poi.xssf.usermodel.XSSFRow.createCell(XSSFRow.java:147)
    at org.apache.poi.xssf.usermodel.XSSFRow.createCell(XSSFRow.java:39)
    at de.h2o.testing.testprotocol.TestProtocol.setCellValue(TestProtocol.java:376)
    at de.h2o.testing.testprotocol.TestProtocol.writeTableHead(TestProtocol.java:216)
    at de.h2o.testing.testprotocol.TestProtocol.writeToFile(TestProtocol.java:113)
    at de.h2o.testing.testprotocol.TestProtocol.afterClass(TestProtocol.java:102)
    at de.h2o.testing.testprotocol.H2oTestRunner$1.finishTestRun(H2oTestRunner.java:85)
    at de.h2o.testing.testprotocol.H2oTestRunner$1.countDownTestCounter(H2oTestRunner.java:119)
    at de.h2o.testing.testprotocol.H2oTestRunner$1.testIgnored(H2oTestRunner.java:113)
    at org.junit.runner.notification.RunNotifier$6.notifyListener(RunNotifier.java:175)
    at org.junit.runner.notification.RunNotifier$SafeNotifier.run(RunNotifier.java:61)
    at org.junit.runner.notification.RunNotifier.fireTestIgnored(RunNotifier.java:172)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at de.h2o.testing.testBase.CompleteTestWatcher$1.evaluate(CompleteTestWatcher.java:16)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at de.h2o.testing.testprotocol.H2oTestRunner.run(H2oTestRunner.java:123)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:80)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:47)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:69)
    at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:49)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
    at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
    at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
    at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:103)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.messaging.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:355)
    at org.gradle.internal.concurrent.DefaultExecutorFactory$StoppableExecutorImpl$1.run(DefaultExecutorFactory.java:66)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:724)

问题不在于任何测试类或testrunner或其他什么,因为在使用.csv文件时一切正常。现在,在更改为xlsx后,我将使用gradle。

希望有人可以帮忙......

这是代码。它在每个测试类中调用。 writeHead,writeData -methods等正在用数据填充工作表。抛出的异常是方法:setCellValue。

private void writeTableHead(Sheet sheet) {

    setCellValue(7, 0, "*******************************");
    // print versionline

    setCellValue(8, 0, "Version");
    int cellCount = 4;
    for (TestRun runTriple : summary.getTestRuns()) {
        setCellValue(8, cellCount, runTriple.version);
        setStyle(8, cellCount, styles.get("borderThin"));
        sheet.setColumnWidth(cellCount, 2500);
        cellCount++;
    }
    setCellValue(9, 0, "Build");
    cellCount = 4;
    for (TestRun runTriple : summary.getTestRuns()) {
        setCellValue(9, cellCount, runTriple.build);
        setStyle(9, cellCount, styles.get("borderThin"));
        cellCount++;
    }
    setCellValue(10, 0, "Package");
    setCellValue(10, 1, "Class");
    setCellValue(10, 2, "Name");
    setCellValue(10, 3, "Type");
    cellCount = 4;
    for (TestRun runTriple : summary.getTestRuns()) {
        setCellValue(10, cellCount, new SimpleDateFormat("dd.MM.yyyy").format(runTriple.date));
        setStyle(10, cellCount, styles.get("date"));
        cellCount++;
    }

}

private void writeHead(Sheet sheet) {
    double totalFailed = summary.getNumberOfFailedTests();
    double totalSkipped = summary.getNumberOfSkippedTests();
    double totalSucceeded = summary.getNumberOfSucceededTests();
    double totalNumberOfTests = summary.getNumberOfTests();
    double succeededPercent = totalSucceeded / totalNumberOfTests;
    double failedPercent = totalFailed / totalNumberOfTests;
    double skippedPercent = totalSkipped / totalNumberOfTests;

    setCellValue(0, 0, "TESTPROTOKOL");

    setCellValue(1, 0, "Archiv");
    setCellValue(1, 1, "Git repository");

    setCellValue(2, 0, "Anzahl Tests");
    setCellValue(2, 1, totalNumberOfTests);

    setCellValue(3, 0, "Erfolgreiche Tests");
    setCellValue(3, 1, totalSucceeded);
    setCellValue(3, 2, succeededPercent);

    setCellValue(4, 0, "Fehlgeschlagene Tests");
    setCellValue(4, 1, totalFailed);
    setCellValue(4, 2, failedPercent);

    setCellValue(5, 0, "Geskippte Tests");
    setCellValue(5, 1, totalSkipped);
    setCellValue(5, 2, skippedPercent);

}

private void setCellValue(int row, int column, String value) {
    Row tmpRow;
    if (sheet.getRow(row) == null) {
        tmpRow = sheet.createRow(row);
    } else {
        tmpRow = sheet.getRow(row);
    }
    tmpRow.createCell(column).setCellValue(value);
}



private void writeToFile() {

    try {

        // Write protocolHEAD
        writeHead(sheet);

        // Write tableHEAD
        writeTableHead(sheet);

        // print data
        writeData(sheet);
        formatSheet(sheet);

        FileOutputStream stream = new FileOutputStream(file);
        wb.write(stream);
        stream.flush();
        stream.close();

        System.out.println("Protocol written to " + file.getAbsolutePath());
    } catch (Exception e) {
        log.warning("error occured while writing protocol file: " + e.getClass().getCanonicalName() + ": " + e.getMessage());
        e.printStackTrace();
    }
}

1 个答案:

答案 0 :(得分:0)

主要问题是,gradle尝试并行执行多个测试。因此,第一个测试开始编写文件,因此其他测试阻止访问文件。按顺序运行测试后,一切正常