java - JUnit 中的参数类型不匹配

标签 java eclipse excel selenium junit

我对 Selenium webdriver 和 Java 相当陌生,之前我使用过 Selenium IDE。我正在尝试从 Excel 工作表中读取测试数据。然后将其写入 Eclipse 控制台,该控制台可以工作,并且应该用于执行实际测试,但该测试不起作用。实际测试未执行,因为我收到错误参数类型不匹配。代码如下所示:

package Test_Excel;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

@RunWith(Parameterized.class)
public class TestWithExcelData {

    // Our two parameters
    private final int input;
    private final int resultExpected;

    // Constructor
    public TestWithExcelData(int input, int result) {
        this.input = input;
        this.resultExpected = result;
    }

    @Parameters
    public static Iterable<Object []> data() throws BiffException, IOException
    {

        String FilePath = "C://Selenium//workspace//testproject//src//testdata//TestData.xls";
        FileInputStream fs = new FileInputStream(FilePath);

        Object[][] object = new Object[6][2];
        Workbook wb = Workbook.getWorkbook(fs);
        //locate the excel file in the local machine

        Sheet sheet = wb.getSheet("IOResult");
        int i=1; //avoid header row
        while(!(sheet.getCell(0, i).getContents().equals("end"))) //read data till it reaches the cell whose text is ‘end’
        {

            object[i-1][0]=sheet.getCell(0, i).getContents();
            object[i-1][1]=sheet.getCell(1, i).getContents();
            System.out.print(sheet.getCell(0, i).getContents() + "\t");
            System.out.print(sheet.getCell(1, i).getContents() + "\t");
            System.out.println();
            i++;

        }
        return Arrays.asList(object);
    }

    @Test
    public void testSquareOff(){
        Assert.assertEquals(resultExpected, MathUtils.square(input));
    }
}

有人可以给我指出正确的方向吗?

提前致谢

最佳答案

方法 sheet.getCell(column, row).getContents() 返回一个字符串,但您的构造函数需要 int

您可以修改data()方法中的两行:

object[i-1][0] = Integer.valueOf(sheet.getCell(0, i).getContents());
object[i-1][1] = Integer.valueOf(sheet.getCell(1, i).getContents());

关于java - JUnit 中的参数类型不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31176774/

相关文章:

java - 异常在线程 "main"java.lang.UnsatisfiedLinkError : RunnerClass. parsecmdline(ILjava/lang/String;)V

java - 标准API。带有 OrderBy 表达式的谓词

excel - 如何编写 VBA 脚本来删除包含某个字符串的范围内的所有单元格?

javascript - 使用适用于 Office 的 JavaScript API 获取 Excel 范围

java - 将 JInternalFrame 在 JDesktopPane 中居中无法正常工作

java - 使用 Spring 与 jar 相关的文件

java - Wowza 流媒体引擎 Eclipse server.xml

eclipse - Maven 下载的扩展名为 .lastUpdated

java - 在不应该的时候返回零

java - 将excel的内容写入文本文件