0

我有一个类,它创建 excel 表。为此,我将 poi-3.2.jar 添加到类路径中。我正在使用 Eclipse3.7。
我通过使用 WindowTester Pro 记录工作台操作来生成测试用例。我编辑了生成的测试用例并尝试在其中创建 excel 表,以编写测试结果。

如果我使用来自普通 java 程序的相同 API(在该插件本身内),它可以创建 excel 文件而不会出错。但是,当我尝试从测试用例创建时,出现以下错误:

java.lang.NoClassDefFoundError: org/apache/poi/hssf/usermodel/HSSFWorkbook

我已将所需的 jar 文件添加到 lib 文件夹并添加到构建路径中。为什么它可以在普通的 java 程序中工作,但不能从 WindowTester 生成的测试用例中工作。

这是我的代码:

import com.windowtester.runtime.IUIContext;
import com.windowtester.runtime.swt.UITestCaseSWT;
import com.windowtester.runtime.swt.locator.eclipse.WorkbenchLocator;

public class Configuration extends UITestCaseSWT {

/*
 * @see junit.framework.TestCase#setUp()
 */
protected void setUp() throws Exception {
    super.setUp();
    IUIContext ui = getUI();
    try {
        CreateExcelFile file = new CreateExcelFile();
        file.CreateExcel();
    } catch (Throwable e) {
        System.out.println("***** Exception catched.. *****");
        // fail("Configuration failed");
        e.printStackTrace();
    }
    ui.ensureThat(new WorkbenchLocator().hasFocus());
}

/**
 * Main test method.
 */
public void testConfiguration() throws Exception {

    //Test code
    }

@Override
protected void tearDown() throws Exception {
    super.tearDown();
    // ....
}

}


public class CreateExcelFile {
HSSFRow row;
HSSFRichTextString string;

public void CreateExcel() {
    try {
        String filename = "E:/hello.xls";
        HSSFWorkbook hwb = new HSSFWorkbook();
        HSSFSheet sheet = hwb.createSheet("new sheet");

        HSSFRow rowhead = sheet.createRow((short) 0);
        string = new HSSFRichTextString("TC_Name");
        rowhead.createCell((int) 0).setCellValue(string);
        string = new HSSFRichTextString("Result");
        rowhead.createCell((int) 1).setCellValue(string);

        for (int i = 0; i <= 10; i++) {
            row = sheet.createRow((short) i);
            string = new HSSFRichTextString("TC_Name" + i);
            row.createCell((int) 0).setCellValue(string);
            if (i % 2 == 0) {
                row.createCell((int) 1).setCellValue(true);
            } else {
                row.createCell((int) 1).setCellValue(false);
            }
        }
        FileOutputStream fileOut = new FileOutputStream(filename);
        hwb.write(fileOut);

        fileOut.close();
        System.out.println("Your excel file has been generated!");
    } catch (Exception ex) {
        System.out.println(ex);
    }
}

}

任何人都可以帮助我。

4

1 回答 1

0

我得到了问题的原因。这是因为运行时类路径。我发现的解决方案是在清单编辑器中打开 MANIFEST.MF。然后在运行时选项卡的 Classpath 部分中,添加所需的 JAR。
现在它的工作没有错误。

于 2012-09-18T04:28:05.523 回答