下面列出了怎么用org.testng.internal.ConstructorOrMethod的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Create the test methods that belong to this class (rejects
* all those that belong to a different class).
*/
private ITestNGMethod[] createTestMethods(ITestNGMethod[] methods) {
List<ITestNGMethod> vResult = Lists.newArrayList();
for (ITestNGMethod tm : methods) {
ConstructorOrMethod m = tm.getConstructorOrMethod();
if (m.getDeclaringClass().isAssignableFrom(m_testClass)) {
for (Object o : m_iClass.getInstances(false)) {
log(4, "Adding method " + tm + " on TestClass " + m_testClass);
vResult.add(new TestNGScenario(/* tm.getRealClass(), */ m.getMethod(), m_annotationFinder, m_xmlTest,
o));
}
}
else {
log(4, "Rejecting method " + tm + " for TestClass " + m_testClass);
}
}
ITestNGMethod[] result = vResult.toArray(new ITestNGMethod[vResult.size()]);
return result;
}
protected ITestResult prepareMock(Class<?> tClass, Method method) {
ITestResult result = mock(ITestResult.class);
IClass clazz = mock(IClass.class);
ITestNGMethod testNGMethod = mock(ITestNGMethod.class);
ConstructorOrMethod cm = mock(ConstructorOrMethod.class);
String methodName = method.getName();
when(result.getTestClass()).thenReturn(clazz);
when(result.getTestClass().getRealClass()).thenReturn(tClass);
when(clazz.getName()).thenReturn(this.getClass().getName());
when(result.getMethod()).thenReturn(testNGMethod);
when(cm.getMethod()).thenReturn(method);
when(result.getMethod().getConstructorOrMethod()).thenReturn(cm);
when(testNGMethod.getMethodName()).thenReturn(methodName);
ITestContext context = mock(ITestContext.class);
when(result.getTestContext()).thenReturn(context);
XmlTest xmlTest = new XmlTest();
XmlSuite suite = new XmlSuite();
xmlTest.setXmlSuite(suite);
suite.setListeners(Arrays.asList(VideoListener.class.getName()));
when(context.getCurrentXmlTest()).thenReturn(xmlTest);
return result;
}
@Override
public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {
for (IMethodInstance methodIns : methods) {
ITestNGMethod method = methodIns.getMethod();
ConstructorOrMethod meth = method.getConstructorOrMethod();
Method m = meth.getMethod();
if (m != null) {
DB db = m.getAnnotation(DB.class);
if (db != null) {
TransactionLegacy txn = TransactionLegacy.open(m.getName());
}
}
}
// TODO Auto-generated method stub
return methods;
}
@Before
public void setUp() {
testngListener = spy(new AllureTestListener());
allure = mock(Allure.class);
testngListener.setLifecycle(allure);
ISuite suite = mock(ISuite.class);
when(suite.getName()).thenReturn(DEFAULT_SUITE_NAME);
XmlTest xmlTest = mock(XmlTest.class);
when(xmlTest.getName()).thenReturn(DEFAULT_XML_TEST_NAME);
testContext = mock(ITestContext.class);
when(testContext.getSuite()).thenReturn(suite);
when(testContext.getCurrentXmlTest()).thenReturn(xmlTest);
// mocking test method parameters
ConstructorOrMethod constructorOrMethod = mock(ConstructorOrMethod.class);
when(constructorOrMethod.getMethod()).thenReturn(parametrizedTestMethod(0, null, null, null));
method = mock(ITestNGMethod.class);
when(method.getConstructorOrMethod()).thenReturn(constructorOrMethod);
testResult = mock(ITestResult.class);
when(testResult.getMethod()).thenReturn(method);
when(testResult.getParameters()).thenReturn(new Object[]{});
IClass iClass = mock(IClass.class);
when(testResult.getTestClass()).thenReturn(iClass);
}
private Optional<Method> getMethod(final ITestNGMethod method) {
return Optional.ofNullable(method)
.map(ITestNGMethod::getConstructorOrMethod)
.map(ConstructorOrMethod::getMethod);
}
/**
*
* @param method method to be described
* @return FQN of a class + method declaration for a method passed in
* ie. test.triangle.CheckCount.testCheckCount(java.lang.String)
*/
private String getMethodDeclaration(ITestNGMethod method) {
//see Utils.detailedMethodName
//perhaps should rather adopt the original method instead
ConstructorOrMethod m = method.getConstructorOrMethod();
StringBuilder buf = new StringBuilder();
buf.append("\"");
if (suiteName != null) {
buf.append(suiteName);
} else {
buf.append("UNKNOWN");
}
buf.append("\"");
buf.append(" - ");
if (method.isBeforeSuiteConfiguration()) {
buf.append("@BeforeSuite ");
} else if (method.isBeforeTestConfiguration()) {
buf.append("@BeforeTest ");
} else if (method.isBeforeClassConfiguration()) {
buf.append("@BeforeClass ");
} else if (method.isBeforeGroupsConfiguration()) {
buf.append("@BeforeGroups ");
} else if (method.isBeforeMethodConfiguration()) {
buf.append("@BeforeMethod ");
} else if (method.isAfterMethodConfiguration()) {
buf.append("@AfterMethod ");
} else if (method.isAfterGroupsConfiguration()) {
buf.append("@AfterGroups ");
} else if (method.isAfterClassConfiguration()) {
buf.append("@AfterClass ");
} else if (method.isAfterTestConfiguration()) {
buf.append("@AfterTest ");
} else if (method.isAfterSuiteConfiguration()) {
buf.append("@AfterSuite ");
}
buf.append(m.getDeclaringClass().getName());
buf.append(".");
buf.append(m.getName());
buf.append("(");
int i = 0;
for (Class<?> p : m.getParameterTypes()) {
if (i++ > 0) {
buf.append(", ");
}
buf.append(p.getName());
}
buf.append(")");
return buf.toString();
}