为了账号安全,请及时绑定邮箱和手机立即绑定

使用 Testng xml 运行脚本时出现 NullPointerException

使用 Testng xml 运行脚本时出现 NullPointerException

慕的地10843 2021-07-09 23:15:51
我面临一个不同的问题,当我使用 Testng xml 运行我的脚本时,它会抛出空点错误,但是当我使用“以编程方式运行 Testng”运行脚本时,它运行良好,没有任何问题。代码public void TestCaseExecutor(Class classname) throws ClassNotFoundException {    FunctionLibrary lib = new FunctionLibrary(driver);    // Getting TestName    for (int Tests = 1; Tests < TestData.sheet.getLastRowNum() + 1; Tests++) {        String ClassName = classname.getSimpleName();        String TestName = TestData.sheet                            .getRow(Tests)                            .getCell(0)                            .getStringCellValue()                            .trim();        // Comparing TestNames from sheet        if (ClassName.equals(TestName)) {            // Method Name from Excel            String MethodName = TestData.sheet                                    .getRow(Tests)                                    .getCell(2)                                    .getStringCellValue()                                    .trim();            try {                // parameter count from excel                int parameterCount = (int) TestData.sheet                                            .getRow(Tests)                                            .getCell(3)                                            .getNumericCellValue();                // reading Method names from Functional library                for (Method m : FunctionLibrary.class.getMethods()) {                    // Comparing Method Name with Excel method name                    if (m.getName().equals(MethodName)) {                        // Comparing paraameter Count from both FL and Excel                        if (m.getParameterCount() == parameterCount) {                            // Creating an array with class                            Class<?> param[] = new Class[parameterCount];                            }            } 
查看完整描述

1 回答

?
潇潇雨雨

TA贡献1833条经验 获得超4个赞

假设1TestData是一个类并且sheet是一个静态字段,则NPE的原因是TestData.sheetnull

解决方案:确保TestData.sheet在尝试对其调用方法之前已初始化。

目前还不清楚为什么这个测试在某些情况下有效,而在其他情况下无效,但我的猜测是这个测试依赖于其他一些测试来初始化TestData.sheets,因此测试运行的顺序很重要。那将是一种设计测试用例中的缺陷......


1 - 此假设基于TestData标识符的大写。标准Java命名约定规定类名必须以大写字母开头,变量名必须以小写字母开头。(我注意到你在其他地方没有遵循这个约定......)


查看完整回答
反对 回复 2021-07-14
  • 1 回答
  • 0 关注
  • 159 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信