我面临一个不同的问题,当我使用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];
for (int i = 0; i < m.getParameterCount(); i++) {
// assigning values in to array with parameter type
param[i] = m.getParameterTypes()[i];
}
Method method = FunctionLibrary.class
.getMethod(MethodName, param);
method.invoke(lib, FunctionLibrary
.ParameterData(parameterCount, Tests));
}
} else if (m.getName().equals("")) {
System.out.println("empty method name");
break;
}
}
} catch (InvocationTargetException
| NoSuchMethodException
| IllegalAccessException
| NullPointerException e) {
e.printStackTrace();
assertTrue(false);
}
}
}
}
在我的脚本中调用上述功能
public class TestCase3_Register extends Browser_nav{
@Test
public void register() {
FunctionLibrary obj = new FunctionLibrary(driver);
try {
obj.TestCaseExecutor(this.getClass());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
我的Excel类
public class LoadExcel {
public static int rowCount;
public static XSSFSheet sheet;
public static XSSFWorkbook book;
public static void loadExcelSheet(){
try{
File file=new File("C:\\Users\\DELL\\Desktop\\TestData.xlsx");
FileInputStream fis=new FileInputStream(file);
try {
book=new XSSFWorkbook(file);
sheet=book.getSheet("TestSteps");
rowCount=sheet.getLastRowNum();
} catch (InvalidFormatException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}catch(FileNotFoundException e){
e.printStackTrace();
}
}
}
错误日志:
java.lang.NullPointerException
at lib.FunctionLibrary.TestCaseExecutor(Functionlibrary.java:50)
at testCaseWithKeywordFramework.TestCase3_Register(TestCase_Register.java:21)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
有人知道如何解决这个问题。
答案 0 :(得分:1)
假设 1 ,TestData
是一个类,sheet
是一个静态字段,则产生NPE的原因是TestData.sheet
是{{1} }。
解决方案:在尝试调用null
上的方法之前,请确保已对其进行初始化。
目前尚不清楚为什么该测试在某些情况下会起作用,而在其他情况下则不会,但是我的猜测是,该测试依赖于其他测试来初始化TestData.sheet
,因此,测试的顺序是重要的运行。这可能是测试用例中的设计缺陷...
1-此假设基于TestData.sheets
标识符的大写字母。标准的Java命名约定规定,类名必须以大写字母开头,变量名必须以小写字母开头。 (我注意到您没有在其他地方遵循此约定...)