下面列出了怎么用org.testng.TestNGException的API类实例代码及写法,或者点击链接到github查看源代码。
protected List<ITestNGListener> initialiseListeners() {
List<ITestNGListener> listeners = new LinkedList<>();
String file = "META-INF/services/listeners.txt";
InputStream stream = getStream(file);
if (stream != null) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream))) {
String line;
while ((line = reader.readLine()) != null) {
listeners.add(instantiate(line));
}
} catch (IOException e) {
throw new TestNGException(e);
}
}
return listeners;
}
private static ITestNGListener instantiate(String className) {
if (className == null || className.trim().isEmpty()) {
throw new IllegalArgumentException("Please provide a valid class name");
}
try {
Class<?> clazz = Class.forName(className);
if (!ITestNGListener.class.isAssignableFrom(clazz)) {
throw new IllegalArgumentException(className
+ " does not implement a TestNG listener");
}
return (ITestNGListener) clazz.newInstance();
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
throw new TestNGException(e);
}
}
@Test( dataProvider = "ConfigurableDataProvider", dataProviderClass = AtsDataProvider.class, expectedExceptions = TestNGException.class)
@TestOptions( dataFile = "Test_BasicDataProvider", dataSheet = TestDetails.FIRST_TEST_SCENARIO)
public void dataFile_noFileExtension( String user, String pswd, String subject ) {}
@Test( dataProvider = "ConfigurableDataProvider", dataProviderClass = AtsDataProvider.class, expectedExceptions = TestNGException.class)
@TestOptions( dataFileFolder = TestDetails.DATA_FILES_FOLDER, dataFile = TestDetails.DATA_FILE1
+ "NON_EXISTING_FILE", dataSheet = TestDetails.FIRST_TEST_SCENARIO)
public void dataFile_wrongFile( String user, String pswd, String subject ) {}
@Test( dataProvider = "ConfigurableDataProvider", dataProviderClass = AtsDataProvider.class, expectedExceptions = TestNGException.class)
@TestOptions( dataFile = TestDetails.DATA_FILE2, dataSheet = TestDetails.FIRST_TEST_SCENARIO)
public void dataFileFolder_fromClassAnnotation( String user, String pswd, String subject ) {}
@Test( dataProvider = "ConfigurableDataProvider", dataProviderClass = AtsDataProvider.class, expectedExceptions = TestNGException.class)
@TestOptions( dataSheet = TestDetails.FIRST_TEST_SCENARIO)
public void dataFile_fromClasspath_negative( String user, String pswd, String subject ) {}
/**
* Finds TestNG methods that the specified TestNG method depends upon
* @param m TestNG method
* @param methods list of methods to search for depended upon methods
* @return list of methods that match the criteria
*/
protected static ITestNGMethod[] findDependedUponMethods(ITestNGMethod m,
ITestNGMethod[] methods)
{
String canonicalMethodName = calculateMethodCanonicalName(m);
List<ITestNGMethod> vResult = Lists.newArrayList();
String regexp = null;
for (String fullyQualifiedRegexp : m.getMethodsDependedUpon()) {
boolean foundAtLeastAMethod = false;
if (null != fullyQualifiedRegexp) {
// Escapes $ in regexps as it is not meant for end - line matching, but inner class matches.
regexp = fullyQualifiedRegexp.replace("$", "\\$");
boolean usePackage = regexp.indexOf('.') != -1;
Pattern pattern = Pattern.compile(regexp);
for (ITestNGMethod method : methods) {
ConstructorOrMethod thisMethod = method.getConstructorOrMethod();
String thisMethodName = thisMethod.getName();
String methodName = usePackage ?
calculateMethodCanonicalName(method)
: thisMethodName;
Pair<String, String> cacheKey = Pair.create(regexp, methodName);
Boolean match = MATCH_CACHE.get(cacheKey);
if (match == null) {
match = pattern.matcher(methodName).matches();
MATCH_CACHE.put(cacheKey, match);
}
if (match) {
vResult.add(method);
foundAtLeastAMethod = true;
}
}
}
if (!foundAtLeastAMethod) {
if (m.ignoreMissingDependencies()) {
continue;
}
if (m.isAlwaysRun()) {
continue;
}
Method maybeReferringTo = findMethodByName(m, regexp);
if (maybeReferringTo != null) {
throw new TestNGException(canonicalMethodName + "() is depending on method "
+ maybeReferringTo + ", which is not annotated with @Test or not included.");
}
throw new TestNGException(canonicalMethodName
+ "() depends on nonexistent method " + regexp);
}
}//end for
return vResult.toArray(new ITestNGMethod[vResult.size()]);
}