下面列出了怎么用org.testng.internal.ObjectFactoryImpl的API类实例代码及写法,或者点击链接到github查看源代码。
@ObjectFactory
public IObjectFactory getObjectFactory(ITestContext context) throws Exception {
Class<?> clazz = Class.forName("org.activejpa.enhancer.ActiveJpaAgentLoaderImpl");
Method method = clazz.getMethod("loadAgent");
method.invoke(null);
return new ObjectFactoryImpl();
}
@Test
public void runAllTckTests() throws Throwable {
TestNG testng = new TestNG();
ObjectFactoryImpl delegate = new ObjectFactoryImpl();
testng.setObjectFactory((IObjectFactory) (constructor, params) -> {
if (constructor.getDeclaringClass().equals(ReactiveStreamsCdiTck.class)) {
return tck;
}
else {
return delegate.newInstance(constructor, params);
}
});
testng.setUseDefaultListeners(false);
ResultListener resultListener = new ResultListener();
testng.addListener((ITestNGListener) resultListener);
testng.setTestClasses(new Class[]{ ReactiveStreamsCdiTck.class });
testng.setMethodInterceptor(new IMethodInterceptor() {
@Override
public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {
methods.sort(Comparator.comparing(m -> m.getInstance().getClass().getName()));
return methods;
}
});
testng.run();
int total = resultListener.success.get() + resultListener.failed.get() + resultListener.skipped.get();
System.out.println(String.format("Ran %d tests, %d passed, %d failed, %d skipped.", total, resultListener.success.get(),
resultListener.failed.get(), resultListener.skipped.get()));
System.out.println("Failed tests:");
resultListener.failures.forEach(result -> {
System.out.println(result.getInstance().getClass().getName() + "." + result.getMethod().getMethodName());
});
if (resultListener.failed.get() > 0) {
if (resultListener.lastFailure.get() != null) {
throw resultListener.lastFailure.get();
}
else {
throw new Exception("Tests failed with no exception");
}
}
}
/**
* Note: Kind of hack to ensure that ActiveJPAAgent instruments all the models before they are loaded.
*
* @param context
* @return
* @throws Exception
*/
@ObjectFactory
public IObjectFactory getObjectFactory(ITestContext context) throws Exception {
ActiveJpaAgentLoader.instance().loadAgent();
return new ObjectFactoryImpl();
}