Best Testng code snippet using org.testng.TestRunner.getInjectorFactory
Source:TestRunner.java
...212 m_suite = suite;213 m_testName = test.getName();214 m_host = suite.getHost();215 m_testClassesFromXml = test.getXmlClasses();216 m_injectorFactory = m_configuration.getInjectorFactory();217 setVerbose(test.getVerbose());218 boolean preserveOrder = test.getPreserveOrder();219 IMethodInterceptor builtinInterceptor =220 preserveOrder221 ? new PreserveOrderMethodInterceptor()222 : new InstanceOrderingMethodInterceptor();223 m_methodInterceptors = new ArrayList<>();224 // Add the built in interceptor as the first interceptor. That way we let our users determine225 // the final order226 // by plugging in their own custom interceptors as well.227 m_methodInterceptors.add(builtinInterceptor);228 List<XmlPackage> m_packageNamesFromXml = getAllPackages();229 for (XmlPackage xp : m_packageNamesFromXml) {230 m_testClassesFromXml.addAll(xp.getXmlClasses());231 }232 m_annotationFinder = annotationFinder;233 m_invokedMethodListeners = invokedMethodListeners;234 m_classListeners.clear();235 for (IClassListener classListener : classListeners) {236 m_classListeners.put(classListener.getClass(), classListener);237 }238 m_invoker =239 new Invoker(240 m_configuration,241 this,242 this,243 m_suite.getSuiteState(),244 skipFailedInvocationCounts,245 invokedMethodListeners,246 classListeners, holder);247 if (test.getParallel() != null) {248 log(249 "Running the tests in '" + test.getName() + "' with parallel mode:" + test.getParallel());250 }251 setOutputDirectory(outputDirectory);252 // Finish our initialization253 init();254 }255 /**256 * Returns all packages to use for the current test. This includes the test from the test257 * suite. Never returns null.258 */259 private List<XmlPackage> getAllPackages() {260 final List<XmlPackage> allPackages = Lists.newArrayList();261 final List<XmlPackage> suitePackages = this.m_xmlTest.getSuite().getPackages();262 if (suitePackages != null) {263 allPackages.addAll(suitePackages);264 }265 final List<XmlPackage> testPackages = this.m_xmlTest.getPackages();266 if (testPackages != null) {267 allPackages.addAll(testPackages);268 }269 return allPackages;270 }271 public IInvoker getInvoker() {272 return m_invoker;273 }274 public ITestNGMethod[] getBeforeSuiteMethods() {275 return m_beforeSuiteMethods;276 }277 public ITestNGMethod[] getAfterSuiteMethods() {278 return m_afterSuiteMethods;279 }280 public ITestNGMethod[] getBeforeTestConfigurationMethods() {281 return m_beforeXmlTestMethods;282 }283 public ITestNGMethod[] getAfterTestConfigurationMethods() {284 return m_afterXmlTestMethods;285 }286 private void init() {287 initMetaGroups(m_xmlTest);288 initRunInfo(m_xmlTest);289 // Init methods and class map290 // JUnit behavior is different and doesn't need this initialization step291 if (!m_xmlTest.isJUnit()) {292 initMethods();293 }294 initListeners();295 addConfigurationListener(m_confListener);296 for (IConfigurationListener cl : m_configuration.getConfigurationListeners()) {297 addConfigurationListener(cl);298 }299 }300 private void initListeners() {301 //302 // Find all the listener factories and collect all the listeners requested in a303 // @Listeners annotation.304 //305 Set<Class<? extends ITestNGListener>> listenerClasses = Sets.newHashSet();306 Class<? extends ITestNGListenerFactory> listenerFactoryClass = null;307 for (IClass cls : getTestClasses()) {308 Class<?> realClass = cls.getRealClass();309 TestListenerHelper.ListenerHolder listenerHolder =310 TestListenerHelper.findAllListeners(realClass, m_annotationFinder);311 if (listenerFactoryClass == null) {312 listenerFactoryClass = listenerHolder.getListenerFactoryClass();313 }314 listenerClasses.addAll(listenerHolder.getListenerClasses());315 }316 if (listenerFactoryClass == null) {317 listenerFactoryClass = DefaultListenerFactory.class;318 }319 //320 // Now we have all the listeners collected from @Listeners and at most one321 // listener factory collected from a class implementing ITestNGListenerFactory.322 // Instantiate all the requested listeners.323 //324 ITestNGListenerFactory factory =325 TestListenerHelper.createListenerFactory(m_testClassFinder, listenerFactoryClass);326 // Instantiate all the listeners327 for (Class<? extends ITestNGListener> c : listenerClasses) {328 if (IClassListener.class.isAssignableFrom(c) && m_classListeners.containsKey(c)) {329 continue;330 }331 ITestNGListener listener = factory.createListener(c);332 addListener(listener);333 }334 }335 /** Initialize meta groups */336 private void initMetaGroups(XmlTest xmlTest) {337 Map<String, List<String>> metaGroups = xmlTest.getMetaGroups();338 for (Map.Entry<String, List<String>> entry : metaGroups.entrySet()) {339 addMetaGroup(entry.getKey(), entry.getValue());340 }341 }342 private void initRunInfo(final XmlTest xmlTest) {343 // Groups344 m_xmlMethodSelector.setIncludedGroups(createGroups(m_xmlTest.getIncludedGroups()));345 m_xmlMethodSelector.setExcludedGroups(createGroups(m_xmlTest.getExcludedGroups()));346 m_xmlMethodSelector.setScript(m_xmlTest.getScript());347 // Groups override348 m_xmlMethodSelector.setOverrideIncludedMethods(m_configuration.getOverrideIncludedMethods());349 // Methods350 m_xmlMethodSelector.setXmlClasses(m_xmlTest.getXmlClasses());351 m_runInfo.addMethodSelector(m_xmlMethodSelector, 10);352 // Add user-specified method selectors (only class selectors, we can ignore353 // script selectors here)354 if (null != xmlTest.getMethodSelectors()) {355 for (org.testng.xml.XmlMethodSelector selector : xmlTest.getMethodSelectors()) {356 if (selector.getClassName() != null) {357 IMethodSelector s = InstanceCreator.createSelector(selector);358 m_runInfo.addMethodSelector(s, selector.getPriority());359 }360 }361 }362 }363 private void initMethods() {364 //365 // Calculate all the methods we need to invoke366 //367 List<ITestNGMethod> beforeClassMethods = Lists.newArrayList();368 List<ITestNGMethod> testMethods = Lists.newArrayList();369 List<ITestNGMethod> afterClassMethods = Lists.newArrayList();370 List<ITestNGMethod> beforeSuiteMethods = Lists.newArrayList();371 List<ITestNGMethod> afterSuiteMethods = Lists.newArrayList();372 List<ITestNGMethod> beforeXmlTestMethods = Lists.newArrayList();373 List<ITestNGMethod> afterXmlTestMethods = Lists.newArrayList();374 ClassInfoMap classMap = new ClassInfoMap(m_testClassesFromXml);375 m_testClassFinder =376 new TestNGClassFinder(377 classMap, Maps.newHashMap(), m_configuration, this, holder);378 ITestMethodFinder testMethodFinder =379 new TestNGMethodFinder(m_runInfo, m_annotationFinder, comparator);380 m_runInfo.setTestMethods(testMethods);381 //382 // Initialize TestClasses383 //384 IClass[] classes = m_testClassFinder.findTestClasses();385 for (IClass ic : classes) {386 // Create TestClass387 ITestClass tc =388 new TestClass(389 ic,390 testMethodFinder,391 m_annotationFinder,392 m_xmlTest,393 classMap.getXmlClass(ic.getRealClass()), m_testClassFinder.getFactoryCreationFailedMessage());394 m_classMap.put(ic.getRealClass(), tc);395 }396 //397 // Calculate groups methods398 //399 Map<String, List<ITestNGMethod>> beforeGroupMethods =400 MethodGroupsHelper.findGroupsMethods(m_classMap.values(), true);401 Map<String, List<ITestNGMethod>> afterGroupMethods =402 MethodGroupsHelper.findGroupsMethods(m_classMap.values(), false);403 //404 // Walk through all the TestClasses, store their method405 // and initialize them with the correct ITestClass406 //407 for (ITestClass tc : m_classMap.values()) {408 fixMethodsWithClass(tc.getTestMethods(), tc, testMethods);409 fixMethodsWithClass(((ITestClassConfigInfo) tc).getAllBeforeClassMethods().toArray(new ITestNGMethod[0]), tc, beforeClassMethods);410 fixMethodsWithClass(tc.getBeforeTestMethods(), tc, null);411 fixMethodsWithClass(tc.getAfterTestMethods(), tc, null);412 fixMethodsWithClass(tc.getAfterClassMethods(), tc, afterClassMethods);413 fixMethodsWithClass(tc.getBeforeSuiteMethods(), tc, beforeSuiteMethods);414 fixMethodsWithClass(tc.getAfterSuiteMethods(), tc, afterSuiteMethods);415 fixMethodsWithClass(tc.getBeforeTestConfigurationMethods(), tc, beforeXmlTestMethods);416 fixMethodsWithClass(tc.getAfterTestConfigurationMethods(), tc, afterXmlTestMethods);417 fixMethodsWithClass(418 tc.getBeforeGroupsMethods(),419 tc,420 MethodHelper.uniqueMethodList(beforeGroupMethods.values()));421 fixMethodsWithClass(422 tc.getAfterGroupsMethods(),423 tc,424 MethodHelper.uniqueMethodList(afterGroupMethods.values()));425 }426 //427 // Sort the methods428 //429 m_beforeSuiteMethods =430 MethodHelper.collectAndOrderMethods(431 beforeSuiteMethods,432 false /* forTests */,433 m_runInfo,434 m_annotationFinder,435 true /* unique */,436 m_excludedMethods,437 comparator);438 m_beforeXmlTestMethods =439 MethodHelper.collectAndOrderMethods(440 beforeXmlTestMethods,441 false /* forTests */,442 m_runInfo,443 m_annotationFinder,444 true /* unique (CQ added by me)*/,445 m_excludedMethods,446 comparator);447 m_classMethodMap = new ClassMethodMap(Arrays.asList(testMethodsContainer.getItems()), m_xmlMethodSelector);448 m_groupMethods = new ConfigurationGroupMethods(testMethodsContainer, beforeGroupMethods, afterGroupMethods);449 m_afterXmlTestMethods =450 MethodHelper.collectAndOrderMethods(451 afterXmlTestMethods,452 false /* forTests */,453 m_runInfo,454 m_annotationFinder,455 true /* unique (CQ added by me)*/,456 m_excludedMethods,457 comparator);458 m_afterSuiteMethods =459 MethodHelper.collectAndOrderMethods(460 afterSuiteMethods,461 false /* forTests */,462 m_runInfo,463 m_annotationFinder,464 true /* unique */,465 m_excludedMethods,466 comparator);467 }468 private ITestNGMethod[] computeAndGetAllTestMethods() {469 List<ITestNGMethod> testMethods = Lists.newArrayList();470 for (ITestClass tc : m_classMap.values()) {471 fixMethodsWithClass(tc.getTestMethods(), tc, testMethods);472 }473 return MethodHelper.collectAndOrderMethods(474 testMethods,475 true /* forTest? */,476 m_runInfo,477 m_annotationFinder,478 false /* unique */,479 m_excludedMethods,480 comparator);481 }482 public Collection<ITestClass> getTestClasses() {483 return m_classMap.values();484 }485 public void setTestName(String name) {486 m_testName = name;487 }488 public void setOutputDirectory(String od) {489 m_outputDirectory = od;490 }491 private void addMetaGroup(String name, List<String> groupNames) {492 m_metaGroups.put(name, groupNames);493 }494 private Map<String, String> createGroups(List<String> groups) {495 return GroupsHelper.createGroups(m_metaGroups, groups);496 }497 /**498 * The main entry method for TestRunner.499 *500 * <p>This is where all the hard work is done: - Invoke configuration methods - Invoke test501 * methods - Catch exceptions - Collect results - Invoke listeners - etc...502 */503 public void run() {504 beforeRun();505 try {506 XmlTest test = getTest();507 if (test.isJUnit()) {508 privateRunJUnit();509 } else {510 privateRun(test);511 }512 } finally {513 afterRun();514 forgetHeavyReferencesIfNeeded();515 }516 }517 private void forgetHeavyReferencesIfNeeded() {518 if (RuntimeBehavior.isMemoryFriendlyMode()) {519 testMethodsContainer.clearItems();520 m_groupMethods = null;521 m_classMethodMap = null;522 }523 }524 /** Before run preparements. */525 private void beforeRun() {526 //527 // Log the start date528 //529 m_startDate = new Date(System.currentTimeMillis());530 // Log start531 logStart();532 // Invoke listeners533 fireEvent(true /*start*/);534 // invoke @BeforeTest535 ITestNGMethod[] testConfigurationMethods = getBeforeTestConfigurationMethods();536 invokeTestConfigurations(testConfigurationMethods);537 }538 private void invokeTestConfigurations(ITestNGMethod[] testConfigurationMethods) {539 if (null != testConfigurationMethods && testConfigurationMethods.length > 0) {540 ConfigMethodArguments arguments = new Builder()541 .usingConfigMethodsAs(testConfigurationMethods)542 .forSuite(m_xmlTest.getSuite())543 .usingParameters(m_xmlTest.getAllParameters())544 .build();545 m_invoker.getConfigInvoker().invokeConfigurations(arguments);546 }547 }548 private ITestNGMethod[] m_allJunitTestMethods = new ITestNGMethod[] {};549 private void privateRunJUnit() {550 final ClassInfoMap cim = new ClassInfoMap(m_testClassesFromXml, false);551 final Set<Class<?>> classes = cim.getClasses();552 final List<ITestNGMethod> runMethods = Lists.newArrayList();553 List<IWorker<ITestNGMethod>> workers = Lists.newArrayList();554 // FIXME: directly referencing JUnitTestRunner which uses JUnit classes555 // may result in an class resolution exception under different JVMs556 // The resolution process is not specified in the JVM spec with a specific implementation,557 // so it can be eager => failure558 workers.add(559 new IWorker<ITestNGMethod>() {560 /** @see TestMethodWorker#getTimeOut() */561 @Override562 public long getTimeOut() {563 return 0;564 }565 /** @see java.lang.Runnable#run() */566 @Override567 public void run() {568 for (Class<?> tc : classes) {569 List<XmlInclude> includedMethods = cim.getXmlClass(tc).getIncludedMethods();570 List<String> methods = Lists.newArrayList();571 for (XmlInclude inc : includedMethods) {572 methods.add(inc.getName());573 }574 IJUnitTestRunner tr = IJUnitTestRunner.createTestRunner(TestRunner.this);575 tr.setInvokedMethodListeners(m_invokedMethodListeners);576 try {577 tr.run(tc, methods.toArray(new String[0]));578 } catch (Exception ex) {579 LOGGER.error(ex.getMessage(), ex);580 } finally {581 runMethods.addAll(tr.getTestMethods());582 }583 }584 }585 @Override586 public List<ITestNGMethod> getTasks() {587 throw new TestNGException("JUnit not supported");588 }589 @Override590 public int getPriority() {591 if (m_allJunitTestMethods.length == 1) {592 return m_allJunitTestMethods[0].getPriority();593 } else {594 return 0;595 }596 }597 @Override598 public int compareTo(@Nonnull IWorker<ITestNGMethod> other) {599 return getPriority() - other.getPriority();600 }601 });602 runJUnitWorkers(workers);603 m_allJunitTestMethods = runMethods.toArray(new ITestNGMethod[0]);604 }605 private static Comparator<ITestNGMethod> newComparator(boolean needPrioritySort) {606 return needPrioritySort ? new TestMethodComparator() : null;607 }608 private boolean sortOnPriority(ITestNGMethod[] interceptedOrder) {609 return m_methodInterceptors.size() > 1 ||610 Arrays.stream(interceptedOrder).anyMatch(m -> m.getPriority() != 0);611 }612 // If any of the test methods specify a priority other than the default, we'll need to be able to sort them.613 private static BlockingQueue<Runnable> newQueue(boolean needPrioritySort) {614 return needPrioritySort ? new PriorityBlockingQueue<>() : new LinkedBlockingQueue<>();615 }616 /**617 * Main method that create a graph of methods and then pass it to the graph executor to run them.618 */619 private void privateRun(XmlTest xmlTest) {620 boolean parallel = xmlTest.getParallel().isParallel();621 // parallel622 int threadCount = parallel ? xmlTest.getThreadCount() : 1;623 // Make sure we create a graph based on the intercepted methods, otherwise an interceptor624 // removing methods would cause the graph never to terminate (because it would expect625 // termination from methods that never get invoked).626 ITestNGMethod[] interceptedOrder = intercept(getAllTestMethods());627 AtomicReference<IDynamicGraph<ITestNGMethod>> reference = new AtomicReference<>();628 TimeUtils.computeAndShowTime("DynamicGraphHelper.createDynamicGraph()",629 () -> {630 IDynamicGraph<ITestNGMethod> ref = DynamicGraphHelper631 .createDynamicGraph(interceptedOrder, getCurrentXmlTest());632 reference.set(ref);633 }634 );635 IDynamicGraph<ITestNGMethod> graph = reference.get();636 graph.setVisualisers(this.visualisers);637 // In some cases, additional sorting is needed to make sure tests run in the appropriate order.638 // If the user specified a method interceptor, or if we have any methods that have a non-default639 // priority on them, we need to sort.640 boolean needPrioritySort = sortOnPriority(interceptedOrder);641 Comparator<ITestNGMethod> methodComparator = newComparator(needPrioritySort);642 if (parallel) {643 if (graph.getNodeCount() <= 0) {644 return;645 }646 ITestNGThreadPoolExecutor executor =647 this.m_configuration.getExecutorFactory().newTestMethodExecutor(648 "test=" + xmlTest.getName(),649 graph,650 this,651 threadCount,652 threadCount,653 0,654 TimeUnit.MILLISECONDS,655 newQueue(needPrioritySort),656 methodComparator);657 executor.run();658 try {659 long timeOut = m_xmlTest.getTimeOut(XmlTest.DEFAULT_TIMEOUT_MS);660 Utils.log(661 "TestRunner",662 2,663 "Starting executor for test "664 + m_xmlTest.getName()665 + " with time out:"666 + timeOut667 + " milliseconds.");668 executor.awaitTermination(timeOut, TimeUnit.MILLISECONDS);669 executor.shutdownNow();670 } catch (InterruptedException handled) {671 LOGGER.error(handled.getMessage(), handled);672 Thread.currentThread().interrupt();673 }674 return;675 }676 List<ITestNGMethod> freeNodes = graph.getFreeNodes();677 if (graph.getNodeCount() > 0 && freeNodes.isEmpty()) {678 throw new TestNGException("No free nodes found in:" + graph);679 }680 while (!freeNodes.isEmpty()) {681 if (needPrioritySort) {682 freeNodes.sort(methodComparator);683 // Since this is sequential, let's run one at a time and fetch/sort freeNodes after each method.684 // Future task: To optimize this, we can only update freeNodes after running a test that another test is dependent upon.685 freeNodes = freeNodes.subList(0, 1);686 }687 createWorkers(freeNodes).forEach(Runnable::run);688 graph.setStatus(freeNodes, IDynamicGraph.Status.FINISHED);689 freeNodes = graph.getFreeNodes();690 }691 }692 /** Apply the method interceptor (if applicable) to the list of methods. */693 private ITestNGMethod[] intercept(ITestNGMethod[] methods) {694 List<IMethodInstance> methodInstances =695 MethodHelper.methodsToMethodInstances(Arrays.asList(methods));696 for (IMethodInterceptor m_methodInterceptor : m_methodInterceptors) {697 methodInstances = m_methodInterceptor.intercept(methodInstances, this);698 }699 List<ITestNGMethod> result = MethodHelper.methodInstancesToMethods(methodInstances);700 // Since an interceptor is involved, we would need to ensure that the ClassMethodMap object is701 // in sync with the702 // output of the interceptor, else @AfterClass doesn't get executed at all when interceptors are703 // involved.704 // so let's update the current classMethodMap object with the list of methods obtained from the705 // interceptor.706 this.m_classMethodMap = new ClassMethodMap(result, null);707 ITestNGMethod[] resultArray = result.toArray(new ITestNGMethod[0]);708 // Check if an interceptor had altered the effective test method count. If yes, then we need to709 // update our configurationGroupMethod object with that information.710 if (resultArray.length != testMethodsContainer.getItems().length) {711 m_groupMethods =712 new ConfigurationGroupMethods(713 new TestMethodContainer(() -> resultArray),714 m_groupMethods.getBeforeGroupsMethods(),715 m_groupMethods.getAfterGroupsMethods());716 }717 // If the user specified a method interceptor, whatever that returns is the order we're going718 // to run things in. Set the intercepted priority for that case.719 // There's a built-in interceptor, so look for more than one.720 if (m_methodInterceptors.size() > 1) {721 for (int i = 0; i < resultArray.length; ++i) {722 resultArray[i].setInterceptedPriority(i);723 }724 }725 return resultArray;726 }727 /**728 * Create a list of workers to run the methods passed in parameter. Each test method is run in its729 * own worker except in the following cases: - The method belongs to a class that730 * has @Test(sequential=true) - The parallel attribute is set to "classes" In both these cases,731 * all the methods belonging to that class will then be put in the same worker in order to run in732 * the same thread.733 */734 @Override735 public List<IWorker<ITestNGMethod>> createWorkers(List<ITestNGMethod> methods) {736 AbstractParallelWorker.Arguments args =737 new AbstractParallelWorker.Arguments.Builder()738 .classMethodMap(this.m_classMethodMap)739 .configMethods(this.m_groupMethods)740 .finder(this.m_annotationFinder)741 .invoker(this.m_invoker)742 .methods(methods)743 .testContext(this)744 .listeners(this.m_classListeners.values())745 .build();746 return AbstractParallelWorker.newWorker(m_xmlTest.getParallel(), m_xmlTest.getGroupByInstances()).createWorkers(args);747 }748 //749 // Invoke the workers750 //751 private void runJUnitWorkers(List<? extends IWorker<ITestNGMethod>> workers) {752 // Sequential run753 workers.forEach(Runnable::run);754 }755 private void afterRun() {756 // invoke @AfterTest757 ITestNGMethod[] testConfigurationMethods = getAfterTestConfigurationMethods();758 invokeTestConfigurations(testConfigurationMethods);759 //760 // Log the end date761 //762 m_endDate = new Date(System.currentTimeMillis());763 dumpInvokedMethods();764 // Invoke listeners765 fireEvent(false /*stop*/);766 }767 /** Logs the beginning of the {@link #beforeRun()} . */768 private void logStart() {769 log(770 "Running test "771 + m_testName772 + " on "773 + m_classMap.size()774 + " "775 + " classes, "776 + " included groups:["777 + Strings.valueOf(m_xmlMethodSelector.getIncludedGroups())778 + "] excluded groups:["779 + Strings.valueOf(m_xmlMethodSelector.getExcludedGroups())780 + "]");781 if (getVerbose() >= 3) {782 for (ITestClass tc : m_classMap.values()) {783 ((TestClass) tc).dump();784 }785 }786 }787 /**788 * Trigger the start/finish event.789 *790 * @param isStart <tt>true</tt> if the event is for start, <tt>false</tt> if the event is for791 * finish792 */793 private void fireEvent(boolean isStart) {794 for (ITestListener itl : m_testListeners) {795 if (isStart) {796 itl.onStart(this);797 } else {798 itl.onFinish(this);799 }800 }801 }802 /////803 // ITestContext804 //805 @Override806 public String getName() {807 return m_testName;808 }809 /** @return Returns the startDate. */810 @Override811 public Date getStartDate() {812 return m_startDate;813 }814 /** @return Returns the endDate. */815 @Override816 public Date getEndDate() {817 return m_endDate;818 }819 @Override820 public IResultMap getPassedTests() {821 return m_passedTests;822 }823 @Override824 public IResultMap getSkippedTests() {825 return m_skippedTests;826 }827 @Override828 public IResultMap getFailedTests() {829 return m_failedTests;830 }831 @Override832 public IResultMap getFailedButWithinSuccessPercentageTests() {833 return m_failedButWithinSuccessPercentageTests;834 }835 @Override836 public String[] getIncludedGroups() {837 Map<String, String> ig = m_xmlMethodSelector.getIncludedGroups();838 return ig.values().toArray(new String[0]);839 }840 @Override841 public String[] getExcludedGroups() {842 Map<String, String> eg = m_xmlMethodSelector.getExcludedGroups();843 return eg.values().toArray(new String[0]);844 }845 @Override846 public String getOutputDirectory() {847 return m_outputDirectory;848 }849 /** @return Returns the suite. */850 @Override851 public ISuite getSuite() {852 return m_suite;853 }854 @Override855 public ITestNGMethod[] getAllTestMethods() {856 if (getTest().isJUnit()) {857 //This is true only when we are running JUnit mode858 return m_allJunitTestMethods;859 }860 return testMethodsContainer.getItems();861 }862 @Override863 public String getHost() {864 return m_host;865 }866 @Override867 public Collection<ITestNGMethod> getExcludedMethods() {868 Map<ITestNGMethod, ITestNGMethod> vResult = Maps.newHashMap();869 for (ITestNGMethod m : m_excludedMethods) {870 vResult.put(m, m);871 }872 return vResult.keySet();873 }874 /** @see org.testng.ITestContext#getFailedConfigurations() */875 @Override876 public IResultMap getFailedConfigurations() {877 return m_failedConfigurations;878 }879 @Override880 public IResultMap getConfigurationsScheduledForInvocation() {881 return m_configsToBeInvoked;882 }883 /** @see org.testng.ITestContext#getPassedConfigurations() */884 @Override885 public IResultMap getPassedConfigurations() {886 return m_passedConfigurations;887 }888 /** @see org.testng.ITestContext#getSkippedConfigurations() */889 @Override890 public IResultMap getSkippedConfigurations() {891 return m_skippedConfigurations;892 }893 @Override894 public void addPassedTest(ITestNGMethod tm, ITestResult tr) {895 m_passedTests.addResult(tr);896 }897 @Override898 public Set<ITestResult> getPassedTests(ITestNGMethod tm) {899 return m_passedTests.getResults(tm);900 }901 @Override902 public Set<ITestResult> getFailedTests(ITestNGMethod tm) {903 return m_failedTests.getResults(tm);904 }905 @Override906 public Set<ITestResult> getSkippedTests(ITestNGMethod tm) {907 return m_skippedTests.getResults(tm);908 }909 @Override910 public void addSkippedTest(ITestNGMethod tm, ITestResult tr) {911 m_skippedTests.addResult(tr);912 }913 @Override914 public void addFailedTest(ITestNGMethod testMethod, ITestResult result) {915 logFailedTest(result, false /* withinSuccessPercentage */);916 }917 @Override918 public void addFailedButWithinSuccessPercentageTest(919 ITestNGMethod testMethod, ITestResult result) {920 logFailedTest(result, true /* withinSuccessPercentage */);921 }922 @Override923 public XmlTest getTest() {924 return m_xmlTest;925 }926 @Override927 public List<ITestListener> getTestListeners() {928 return m_testListeners;929 }930 @Override931 public List<IConfigurationListener> getConfigurationListeners() {932 List<IConfigurationListener> listeners = Lists.newArrayList(m_configurationListeners);933 for (IConfigurationListener each : this.m_configuration.getConfigurationListeners()) {934 boolean duplicate = false;935 for (IConfigurationListener listener : listeners) {936 if (each.getClass().equals(listener.getClass())) {937 duplicate = true;938 break;939 }940 }941 if (!duplicate) {942 listeners.add(each);943 }944 }945 return Lists.newArrayList(listeners);946 }947 private void logFailedTest(ITestResult tr, boolean withinSuccessPercentage) {948 if (withinSuccessPercentage) {949 m_failedButWithinSuccessPercentageTests.addResult(tr);950 } else {951 m_failedTests.addResult(tr);952 }953 }954 private static void log(String s) {955 Utils.log("TestRunner", 3, s);956 }957 public static int getVerbose() {958 return m_verbose;959 }960 public void setVerbose(int n) {961 m_verbose = n;962 }963 // TODO: This method needs to be removed and we need to be leveraging addListener().964 // Investigate and fix this.965 void addTestListener(ITestListener listener) {966 Optional<ITestListener> found = m_testListeners.stream()967 .filter(iTestListener -> iTestListener.getClass().equals(listener.getClass()))968 .findAny();969 if (found.isPresent()) {970 return;971 }972 m_testListeners.add(listener);973 }974 public void addListener(ITestNGListener listener) {975 // TODO a listener may be added many times if it implements many interfaces976 if (listener instanceof IMethodInterceptor) {977 m_methodInterceptors.add((IMethodInterceptor) listener);978 }979 if (listener instanceof ITestListener) {980 // At this point, the field m_testListeners has already been used in the creation981 addTestListener((ITestListener) listener);982 }983 if (listener instanceof IClassListener) {984 IClassListener classListener = (IClassListener) listener;985 if (!m_classListeners.containsKey(classListener.getClass())) {986 m_classListeners.put(classListener.getClass(), classListener);987 }988 }989 if (listener instanceof IConfigurationListener) {990 addConfigurationListener((IConfigurationListener) listener);991 }992 if (listener instanceof IConfigurable) {993 m_configuration.setConfigurable((IConfigurable) listener);994 }995 if (listener instanceof IHookable) {996 m_configuration.setHookable((IHookable) listener);997 }998 if (listener instanceof IExecutionListener) {999 IExecutionListener iel = (IExecutionListener) listener;1000 if (m_configuration.addExecutionListenerIfAbsent(iel)) {1001 iel.onExecutionStart();1002 }1003 }1004 if (listener instanceof IDataProviderListener) {1005 IDataProviderListener dataProviderListener = (IDataProviderListener) listener;1006 holder.addListener(dataProviderListener);1007 }1008 if (listener instanceof IDataProviderInterceptor) {1009 IDataProviderInterceptor interceptor = (IDataProviderInterceptor) listener;1010 holder.addInterceptor(interceptor);1011 }1012 if (listener instanceof IExecutionVisualiser) {1013 IExecutionVisualiser l = (IExecutionVisualiser) listener;1014 visualisers.add(l);1015 }1016 m_suite.addListener(listener);1017 }1018 void addConfigurationListener(IConfigurationListener icl) {1019 m_configurationListeners.add(icl);1020 }1021 private void dumpInvokedMethods() {1022 MethodHelper.dumpInvokedMethodInfoToConsole(getAllTestMethods(), getVerbose());1023 }1024 private final IResultMap m_passedConfigurations = new ResultMap();1025 private final IResultMap m_skippedConfigurations = new ResultMap();1026 private final IResultMap m_failedConfigurations = new ResultMap();1027 private final IResultMap m_configsToBeInvoked = new ResultMap();1028 private class ConfigurationListener implements IConfigurationListener {1029 @Override1030 public void beforeConfiguration(ITestResult tr) {1031 m_configsToBeInvoked.addResult(tr);1032 }1033 @Override1034 public void onConfigurationFailure(ITestResult itr) {1035 m_failedConfigurations.addResult(itr);1036 removeConfigurationResultAfterExecution(itr);1037 }1038 @Override1039 public void onConfigurationSkip(ITestResult itr) {1040 m_skippedConfigurations.addResult(itr);1041 removeConfigurationResultAfterExecution(itr);1042 }1043 @Override1044 public void onConfigurationSuccess(ITestResult itr) {1045 m_passedConfigurations.addResult(itr);1046 removeConfigurationResultAfterExecution(itr);1047 }1048 private void removeConfigurationResultAfterExecution(ITestResult itr) {1049 //The remove method of ResultMap removes based on hashCode1050 //So lets find the result based on the method and remove it off.1051 m_configsToBeInvoked.getAllResults().removeIf(tr -> tr.getMethod().equals(itr.getMethod()));1052 }1053 }1054 void addMethodInterceptor(IMethodInterceptor methodInterceptor) {1055 //avoid to add interceptor twice when the defined listeners implements both ITestListener and IMethodInterceptor.1056 if (!m_methodInterceptors.contains(methodInterceptor)) {1057 m_methodInterceptors.add(methodInterceptor);1058 }1059 }1060 @Override1061 public XmlTest getCurrentXmlTest() {1062 return m_xmlTest;1063 }1064 private final IAttributes m_attributes = new Attributes();1065 @Override1066 public Object getAttribute(String name) {1067 return m_attributes.getAttribute(name);1068 }1069 @Override1070 public void setAttribute(String name, Object value) {1071 m_attributes.setAttribute(name, value);1072 }1073 @Override1074 public Set<String> getAttributeNames() {1075 return m_attributes.getAttributeNames();1076 }1077 @Override1078 public Object removeAttribute(String name) {1079 return m_attributes.removeAttribute(name);1080 }1081 private final ListMultiMap<Class<? extends Module>, Module> m_guiceModules = Maps.newListMultiMap();1082 @Override1083 public List<Module> getGuiceModules(Class<? extends Module> cls) {1084 return m_guiceModules.get(cls);1085 }1086 @Override1087 public List<Module> getAllGuiceModules() {1088 return m_guiceModules.values().1089 stream()1090 .flatMap(Collection::stream)1091 .collect(Collectors.toList());1092 }1093 @Override1094 public void addGuiceModule(Module module) {1095 Class<? extends Module> cls = module.getClass();1096 List<Module> modules = m_guiceModules.get(cls);1097 boolean found = modules.stream().anyMatch(each -> each.getClass().equals(cls));1098 if (!found) {1099 modules.add(module);1100 }1101 }1102 private final Map<List<Module>, Injector> m_injectors = Maps.newHashMap();1103 @Override1104 public Injector getInjector(List<Module> moduleInstances) {1105 return m_injectors.get(moduleInstances);1106 }1107 @Override1108 public Injector getInjector(IClass iClass) {1109 if (hasNoGuiceAnnotations(iClass)) {1110 return null;1111 }1112 if (guiceHelper == null) {1113 guiceHelper = new GuiceHelper(this);1114 }1115 return guiceHelper.getInjector(iClass, this.m_injectorFactory);1116 }1117 @Override1118 public void addInjector(List<Module> moduleInstances, Injector injector) {1119 m_injectors.put(moduleInstances, injector);1120 }1121 private static boolean hasNoGuiceAnnotations(IClass iClass) {1122 return AnnotationHelper.findAnnotationSuperClasses(Guice.class, iClass.getRealClass()) == null;1123 }1124 @Override1125 public IInjectorFactory getInjectorFactory() {1126 return this.m_injectorFactory;1127 }1128}...
getInjectorFactory
Using AI Code Generation
1public class TestRunner extends TestNG {2 public static void main(String[] args) {3 TestRunner testRunner = new TestRunner();4 testRunner.setTestClasses(new Class[] {Test.class});5 testRunner.getInjectorFactory();6 }7}8public class TestRunner extends TestNG {9 public static void main(String[] args) {10 TestRunner testRunner = new TestRunner();11 testRunner.setTestClasses(new Class[] {Test.class});12 testRunner.getInjector();13 }14}15public class TestRunner extends TestNG {16 public static void main(String[] args) {17 TestRunner testRunner = new TestRunner();18 testRunner.setTestClasses(new Class[] {Test.class});19 testRunner.getMethodInterceptor();20 }21}22public class TestRunner extends TestNG {23 public static void main(String[] args) {24 TestRunner testRunner = new TestRunner();25 testRunner.setTestClasses(new Class[] {Test.class});26 testRunner.getMethodSelector();27 }28}29public class TestRunner extends TestNG {30 public static void main(String[] args) {31 TestRunner testRunner = new TestRunner();32 testRunner.setTestClasses(new Class[] {Test.class});33 testRunner.getMethodSorter();34 }35}36public class TestRunner extends TestNG {37 public static void main(String[] args) {38 TestRunner testRunner = new TestRunner();39 testRunner.setTestClasses(new Class[] {Test.class});40 testRunner.getMethodValidator();41 }42}43public class TestRunner extends TestNG {44 public static void main(String[] args) {45 TestRunner testRunner = new TestRunner();46 testRunner.setTestClasses(new Class[] {Test.class});47 testRunner.getParameterHandler();48 }49}50public class TestRunner extends TestNG {51 public static void main(String[] args) {52 TestRunner testRunner = new TestRunner();53 testRunner.setTestClasses(new Class[] {Test.class
getInjectorFactory
Using AI Code Generation
1 TestRunner testRunner = new TestRunner();2 InjectorFactory injectorFactory = testRunner.getInjectorFactory();3 Injector injector = injectorFactory.createInjector(new TestNGModule(test));4 ITestNGMethod[] methods = test.getAllTestMethods();5 for (ITestNGMethod method : methods) {6 Object instance = injector.getInstance(method.getRealClass());7 method.setInstances(new Object[] { instance });8 }9 test.setInjector(injector);10 test.setInjectorFactory(injectorFactory);11 }12 public void onFinish(ITestContext context) {13 }14}15package com.automationrhapsody.testng;16import org.testng.IModule;17import org.testng.ITestContext;18import com.google.inject.AbstractModule;19public class TestNGModule extends AbstractModule implements IModule {20 private ITestContext context;21 public TestNGModule(ITestContext context) {22 this.context = context;23 }24 protected void configure() {25 }26 public void applyOptions() {27 }28}
getInjectorFactory
Using AI Code Generation
1public class TestRunner extends TestRunner {2 private final ITestNGMethod m_method;3 private final ITestContext m_testContext;4 private final ITestResult m_testResult;5 public TestRunner(ITestNGMethod method, ITestContext testContext, ITestResult testResult) {6 m_method = method;7 m_testContext = testContext;8 m_testResult = testResult;9 }10 public IInjectorFactory getInjectorFactory() {11 return m_testContext.getInjectorFactory();12 }13}14public class TestRunner extends TestRunner {15 private final ITestNGMethod m_method;16 private final ITestContext m_testContext;17 private final ITestResult m_testResult;18 public TestRunner(ITestNGMethod method, ITestContext testContext, ITestResult testResult) {19 m_method = method;20 m_testContext = testContext;21 m_testResult = testResult;22 }23 public IInjectorFactory getInjectorFactory() {24 return m_testContext.getInjectorFactory();25 }26}27public class TestRunner extends TestRunner {28 private final ITestNGMethod m_method;29 private final ITestContext m_testContext;30 private final ITestResult m_testResult;31 public TestRunner(ITestNGMethod method, ITestContext testContext, ITestResult testResult) {32 m_method = method;33 m_testContext = testContext;34 m_testResult = testResult;35 }36 public IInjectorFactory getInjectorFactory() {37 return m_testContext.getInjectorFactory();38 }39}40public class TestRunner extends TestRunner {41 private final ITestNGMethod m_method;42 private final ITestContext m_testContext;43 private final ITestResult m_testResult;44 public TestRunner(ITestNGMethod method, ITestContext testContext, ITestResult testResult) {45 m_method = method;46 m_testContext = testContext;47 m_testResult = testResult;48 }49 public IInjectorFactory getInjectorFactory() {50 return m_testContext.getInjectorFactory();51 }52}53public class TestRunner extends TestRunner {
getInjectorFactory
Using AI Code Generation
1TestRunner runner = new TestRunner();2IInjectorFactory factory = runner.getInjectorFactory();3System.out.println(factory);4TestRunner runner = new TestRunner();5ITestListener[] listeners = runner.getTestListeners();6System.out.println(listeners);7TestRunner runner = new TestRunner();8String testName = runner.getTestName();9System.out.println(testName);10TestRunner runner = new TestRunner();11ITest test = runner.getTestObject();12System.out.println(test);13TestRunner runner = new TestRunner();14ITestResult result = runner.getTestResult();15System.out.println(result);16TestRunner runner = new TestRunner();17ITestResult[] results = runner.getTestResults();18System.out.println(results);19TestRunner runner = new TestRunner();20ITestRunnerFactory factory = runner.getTestRunnerFactory();21System.out.println(factory);22TestRunner runner = new TestRunner();
getInjectorFactory
Using AI Code Generation
1package test1;2import org.testng.annotations.Test;3public class TestClass1 {4 public void test1() {5 System.out.println("Test1");6 }7}8package test1;9import org.testng.annotations.Test;10public class TestClass2 {11 public void test2() {12 System.out.println("Test2");13 }14}15package test1;16import org.testng.annotations.Test;17public class TestClass3 {18 public void test3() {19 System.out.println("Test3");20 }21}22package test1;23import org.testng.annotations.Test;24public class TestClass4 {25 public void test4() {26 System.out.println("Test4");27 }28}29package test1;30import org.testng.annotations.Test;31public class TestClass5 {32 public void test5() {33 System.out.println("Test5");34 }35}36package test1;37import org.testng.annotations.Test;38public class TestClass6 {39 public void test6() {40 System.out.println("Test6");41 }42}43package test1;44import org.testng.annotations.Test;45public class TestClass7 {46 public void test7() {47 System.out.println("Test7");48 }49}50package test1;51import org.testng.annotations.Test;52public class TestClass8 {53 public void test8() {54 System.out.println("Test8");55 }56}57package test1;58import org.testng.annotations.Test;59public class TestClass9 {60 public void test9() {61 System.out.println("Test9");62 }63}64package test1;65import org.testng.annotations.Test;66public class TestClass10 {67 public void test10() {68 System.out.println("Test10");69 }70}71package test1;72import org.testng.annotations.Test;
TestNG is a Java-based open-source framework for test automation that includes various test types, such as unit testing, functional testing, E2E testing, etc. TestNG is in many ways similar to JUnit and NUnit. But in contrast to its competitors, its extensive features make it a lot more reliable framework. One of the major reasons for its popularity is its ability to structure tests and improve the scripts' readability and maintainability. Another reason can be the important characteristics like the convenience of using multiple annotations, reliance, and priority that make this framework popular among developers and testers for test design. You can refer to the TestNG tutorial to learn why you should choose the TestNG framework.
You can push your abilities to do automated testing using TestNG and advance your career by earning a TestNG certification. Check out our TestNG certification.
Watch this complete tutorial to learn how you can leverage the capabilities of the TestNG framework for Selenium automation testing.
Get 100 minutes of automation test minutes FREE!!