[英]java TestNG calling methods with @Parameters inside @DataProvider
I'm trying to get data using @DataProvider , which returns Object[][]
, in which I'm pasting value by calling another method.我正在尝试使用@DataProvider获取数据,它返回
Object[][]
,其中我通过调用另一个方法粘贴值。 This method, in its turn, using @Parameters to get value from XML.该方法反过来使用@Parameters从XML 获取值。
The problem is that I'm getting NullPointerException
, because in @DataProvider calling required method with @Parameters by passing value null
, hoping that @Parameters will change this value to appropriate one from XML.问题是,我得到
NullPointerException
,因为在@dataProvider通过传递值调用带有@参数所需的方法null
,希望@参数将XML将此值更改为相应的一个。 At the same time, I cannot call method by not passing any arguments to it.同时,我不能通过不传递任何参数来调用方法。
Code:代码:
Class TestSuite
类测试
TestSuite
package blablabla.mainPackage;
import blablabla.framework.*;
import java.io.IOException;
import java.util.ArrayList;
import org.testng.annotations.Test;
public class TestSuite extends Config {
FilesOperations fOps = new FilesOperations();
List<String> fileNames = new ArrayList<String>();
@Test(groups = {"positive"},
dataProvider = "getRandomFileName",
priority = 1)
public void createFileRandom(String fileName) throws IOException {
fOps.createFile(fileName, tempPath);
fileNames.add(fileName);
}
}
Class Config
类
Config
package blablabla.mainPackage;
import blablabla.framework.*;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Parameters;
public class Config extends DataProviders{
public static Path tempPath;
DirectoriesOperations dirOps = new DirectoriesOperations();
@BeforeSuite(alwaysRun = true)
@Parameters({"path"})
public void tearUp(String path) throws IOException {
tempPath = dirOps.createTempDir(Paths.get(path));
}
@AfterSuite(alwaysRun = true, enabled = true)
public void tearDown() throws IOException {
dirOps.deleteTempDirOnExit(tempPath);
}
}
Class DataProviders
类
DataProviders
package blablabla.framework;
import java.io.IOException;
import org.testng.annotations.DataProvider;
public class DataProviders {
FilesOperations fOps = new FilesOperations();
HelpFunctions hf = new HelpFunctions();
ParametrizedFunctions pf = new ParametrizedFunctions();
@DataProvider(name = "getRandomFileName")
public Object[][] getRandomFileName() {
return new Object[][]{new Object[]{pf.generateRandomFileNameWithExtension(null)}};
}
}
Class ParametrizedFunctions
类
ParametrizedFunctions
package blablabla.framework;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.testng.annotations.Parameters;
public class ParametrizedFunctions {
FilesOperations fOps = new FilesOperations();
HelpFunctions hf = new HelpFunctions();
@Parameters({"extensionsArray"})
public String generateRandomFileNameWithExtension(String extensionsArray) {
return fOps.getFileName(hf.stringToArray(extensionsArray), null);
}
}
Here, @Parameters({"extensionsArray"})
does't provide a value from XML file.这里,
@Parameters({"extensionsArray"})
不提供来自 XML 文件的值。 It just takes null
, that was passed as an argument for calling method generateRandomFileNameWithExtension()
.它只需要
null
,它作为调用方法generateRandomFileNameWithExtension()
的参数传递。 And, at the same time, I cannot call this method from @DataProvider without passing any argument to method called.而且,同时,如果不向调用的方法传递任何参数,我无法从@DataProvider调用此方法。
Hope for your suggestions.希望得到您的建议。
I found the solution.我找到了解决方案。 The best way to pass parameter from XML into @DataProvider is passing
ITestContext context
as the argument to the @DataProvider method, and then, get required parameter using:将参数从 XML 传递到@DataProvider的最佳方法是将
ITestContext context
作为参数传递给@DataProvider方法,然后使用以下方法获取所需参数:
context.getCurrentXmlTest().getParameter("parameterName");
Full code of @DataProvider : @DataProvider 的完整代码:
@DataProvider(name = "getRandomFileName")
public Object[][] getRandomFileName(ITestContext context) {
String extensionsArray = context.getCurrentXmlTest().getParameter("extensionsArray");
return new Object[][]{new Object[]{pf.generateRandomFileNameWithExtension(extensionsArray)}};
}
Thanks all for your suggestions.谢谢大家的建议。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.