How to use getRealClass method of org.testng.Interface ITestNGMethod class

Best Testng code snippet using org.testng.Interface ITestNGMethod.getRealClass

Source:Invoker.java Github

copy

Full Screen

...264 handleConfigurationSkip(tm, testResult, annotation, currentTestMethod, instance, suite);265 return;266 }267 Utils.log("", 3, "Failed to invoke configuration method "268 + tm.getRealClass().getName() + "." + tm.getMethodName() + ":" + cause.getMessage());269 handleException(cause, tm, testResult, 1);270 runConfigurationListeners(testResult, false /* after */);271 //272 // If in TestNG mode, need to take a look at the annotation to figure out273 // what kind of @Configuration method we're dealing with274 //275 if (null != annotation) {276 recordConfigurationInvocationFailed(tm, testResult.getTestClass(), annotation, currentTestMethod, instance, suite);277 }278 }279 /**280 * @return All the classes that belong to the same <test> tag as @param cls281 */282 private XmlClass[] findClassesInSameTest(Class<?> cls, XmlSuite suite) {283 Map<String, XmlClass> vResult= Maps.newHashMap();284 String className= cls.getName();285 for(XmlTest test : suite.getTests()) {286 for(XmlClass testClass : test.getXmlClasses()) {287 if(testClass.getName().equals(className)) {288 // Found it, add all the classes in this test in the result289 for(XmlClass thisClass : test.getXmlClasses()) {290 vResult.put(thisClass.getName(), thisClass);291 }292 // Note: we need to iterate through the entire suite since the same293 // class might appear in several <test> tags294 }295 }296 }297 XmlClass[] result= vResult.values().toArray(new XmlClass[vResult.size()]);298 return result;299 }300 /**301 * Record internally the failure of a Configuration, so that we can determine302 * later if @Test should be skipped.303 */304 private void recordConfigurationInvocationFailed(ITestNGMethod tm,305 IClass testClass,306 IConfigurationAnnotation annotation,307 ITestNGMethod currentTestMethod,308 Object instance,309 XmlSuite suite) {310 // If beforeTestClass or afterTestClass failed, mark either the config method's311 // entire class as failed, or the class under tests as failed, depending on312 // the configuration failure policy313 if (annotation.getBeforeTestClass() || annotation.getAfterTestClass()) {314 // tm is the configuration method, and currentTestMethod is null for BeforeClass315 // methods, so we need testClass316 if (m_continueOnFailedConfiguration) {317 setClassInvocationFailure(testClass.getRealClass(), instance);318 } else {319 setClassInvocationFailure(tm.getRealClass(), instance);320 }321 }322 // If before/afterTestMethod failed, mark either the config method's entire323 // class as failed, or just the current test method as failed, depending on324 // the configuration failure policy325 else if (annotation.getBeforeTestMethod() || annotation.getAfterTestMethod()) {326 if (m_continueOnFailedConfiguration) {327 setMethodInvocationFailure(currentTestMethod, instance);328 } else {329 setClassInvocationFailure(tm.getRealClass(), instance);330 }331 }332 // If beforeSuite or afterSuite failed, mark *all* the classes as failed333 // for configurations. At this point, the entire Suite is screwed334 else if (annotation.getBeforeSuite() || annotation.getAfterSuite()) {335 m_suiteState.failed();336 }337 // beforeTest or afterTest: mark all the classes in the same338 // <test> stanza as failed for configuration339 else if (annotation.getBeforeTest() || annotation.getAfterTest()) {340 setClassInvocationFailure(tm.getRealClass(), instance);341 XmlClass[] classes= findClassesInSameTest(tm.getRealClass(), suite);342 for(XmlClass xmlClass : classes) {343 setClassInvocationFailure(xmlClass.getSupportClass(), instance);344 }345 }346 String[] beforeGroups= annotation.getBeforeGroups();347 if(null != beforeGroups && beforeGroups.length > 0) {348 for(String group: beforeGroups) {349 m_beforegroupsFailures.put(group, Boolean.FALSE);350 }351 }352 }353 /**354 * @return true if this class or a parent class failed to initialize.355 */356 private boolean classConfigurationFailed(Class<?> cls) {357 for (Class<?> c : m_classInvocationResults.keySet()) {358 if (c == cls || c.isAssignableFrom(cls)) {359 return true;360 }361 }362 return false;363 }364 /**365 * @return true if this class has successfully run all its @Configuration366 * method or false if at least one of these methods failed.367 */368 private boolean confInvocationPassed(ITestNGMethod method, ITestNGMethod currentTestMethod,369 IClass testClass, Object instance) {370 boolean result= true;371 Class<?> cls = testClass.getRealClass();372 if(m_suiteState.isFailed()) {373 result= false;374 }375 else {376 if (classConfigurationFailed(cls)) {377 if (! m_continueOnFailedConfiguration) {378 result = !classConfigurationFailed(cls);379 } else {380 result = !m_classInvocationResults.get(cls).contains(instance);381 }382 }383 // if method is BeforeClass, currentTestMethod will be null384 else if (m_continueOnFailedConfiguration &&385 currentTestMethod != null &&386 m_methodInvocationResults.containsKey(currentTestMethod)) {387 result = !m_methodInvocationResults.get(currentTestMethod).contains(getMethodInvocationToken(currentTestMethod, instance));388 }389 else if (! m_continueOnFailedConfiguration) {390 for(Class<?> clazz: m_classInvocationResults.keySet()) {391// if (clazz == cls) {392 if(clazz.isAssignableFrom(cls)) {393 result= false;394 break;395 }396 }397 }398 }399 // check if there are failed @BeforeGroups400 String[] groups= method.getGroups();401 if(null != groups && groups.length > 0) {402 for(String group: groups) {403 if(m_beforegroupsFailures.containsKey(group)) {404 result= false;405 break;406 }407 }408 }409 return result;410 }411 // Creates a token for tracking a unique invocation of a method on an instance.412 // Is used when configFailurePolicy=continue.413 private Object getMethodInvocationToken(ITestNGMethod method, Object instance) {414 return String.format("%s+%d", instance.toString(), method.getCurrentInvocationCount());415 }416 /**417 * Effectively invokes a configuration method on all passed in instances.418 * TODO: Should change this method to be more like invokeMethod() so that we can419 * handle calls to {@code IInvokedMethodListener} better.420 *421 * @param targetInstance the instance to invoke the configuration method on422 * @param tm the configuration method423 * @param params the parameters needed for method invocation424 * @param testResult425 * @throws InvocationTargetException426 * @throws IllegalAccessException427 */428 private void invokeConfigurationMethod(Object targetInstance,429 ITestNGMethod tm,430 Object[] params,431 ITestResult testResult)432 throws InvocationTargetException, IllegalAccessException433 {434 // Mark this method with the current thread id435 tm.setId(ThreadUtil.currentThreadInfo());436 {437 InvokedMethod invokedMethod= new InvokedMethod(targetInstance,438 tm,439 params,440 System.currentTimeMillis(),441 testResult);442 runInvokedMethodListeners(BEFORE_INVOCATION, invokedMethod, testResult);443 m_notifier.addInvokedMethod(invokedMethod);444 try {445 Reporter.setCurrentTestResult(testResult);446 Method method = tm.getMethod();447 //448 // If this method is a IConfigurable, invoke its run() method449 //450 IConfigurable configurableInstance =451 IConfigurable.class.isAssignableFrom(tm.getMethod().getDeclaringClass()) ?452 (IConfigurable) targetInstance : m_configuration.getConfigurable();453 if (configurableInstance != null) {454 MethodInvocationHelper.invokeConfigurable(targetInstance, params, configurableInstance, method,455 testResult);456 }457 else {458 //459 // Not a IConfigurable, invoke directly460 //461 if (MethodHelper.calculateTimeOut(tm) <= 0) {462 MethodInvocationHelper.invokeMethod(method, targetInstance, params);463 }464 else {465 MethodInvocationHelper.invokeWithTimeout(tm, targetInstance, params, testResult);466 if (!testResult.isSuccess()) {467 // A time out happened468 throwConfigurationFailure(testResult, testResult.getThrowable());469 throw testResult.getThrowable();470 }471 }472 }473 }474 catch (InvocationTargetException | IllegalAccessException ex) {475 throwConfigurationFailure(testResult, ex);476 throw ex;477 } catch (Throwable ex) {478 throwConfigurationFailure(testResult, ex);479 throw new TestNGException(ex);480 }481 finally {482 Reporter.setCurrentTestResult(testResult);483 runInvokedMethodListeners(AFTER_INVOCATION, invokedMethod, testResult);484 Reporter.setCurrentTestResult(null);485 }486 }487 }488 private void throwConfigurationFailure(ITestResult testResult, Throwable ex)489 {490 testResult.setStatus(ITestResult.FAILURE);;491 testResult.setThrowable(ex.getCause() == null ? ex : ex.getCause());492 }493 private void runInvokedMethodListeners(InvokedMethodListenerMethod listenerMethod, IInvokedMethod invokedMethod,494 ITestResult testResult)495 {496 if ( noListenersPresent() ) {497 return;498 }499 InvokedMethodListenerInvoker invoker = new InvokedMethodListenerInvoker(listenerMethod, testResult, m_testContext);500 for (IInvokedMethodListener currentListener : m_invokedMethodListeners) {501 invoker.invokeListener(currentListener, invokedMethod);502 }503 }504 private boolean noListenersPresent() {505 return (m_invokedMethodListeners == null) || (m_invokedMethodListeners.size() == 0);506 }507 // pass both paramValues and paramIndex to be thread safe in case parallel=true + dataprovider.508 private ITestResult invokeMethod(Object instance,509 final ITestNGMethod tm,510 Object[] parameterValues,511 int parametersIndex,512 XmlSuite suite,513 Map<String, String> params,514 ITestClass testClass,515 ITestNGMethod[] beforeMethods,516 ITestNGMethod[] afterMethods,517 ConfigurationGroupMethods groupMethods,518 FailureContext failureContext) {519 TestResult testResult = new TestResult();520 //521 // Invoke beforeGroups configurations522 //523 invokeBeforeGroupsConfigurations(testClass, tm, groupMethods, suite, params,524 instance);525 //526 // Invoke beforeMethods only if527 // - firstTimeOnly is not set528 // - firstTimeOnly is set, and we are reaching at the first invocationCount529 //530 invokeConfigurations(testClass, tm,531 filterConfigurationMethods(tm, beforeMethods, true /* beforeMethods */),532 suite, params, parameterValues,533 instance, testResult);534 //535 // Create the ExtraOutput for this method536 //537 InvokedMethod invokedMethod = null;538 try {539 testResult.init(testClass, instance,540 tm,541 null,542 System.currentTimeMillis(),543 0,544 m_testContext);545 testResult.setParameters(parameterValues);546 testResult.setHost(m_testContext.getHost());547 testResult.setStatus(ITestResult.STARTED);548 invokedMethod= new InvokedMethod(instance,549 tm,550 parameterValues,551 System.currentTimeMillis(),552 testResult);553 // Fix from ansgarkonermann554 // invokedMethod is used in the finally, which can be invoked if555 // any of the test listeners throws an exception, therefore,556 // invokedMethod must have a value before we get here557 runTestListeners(testResult);558 runInvokedMethodListeners(BEFORE_INVOCATION, invokedMethod, testResult);559 m_notifier.addInvokedMethod(invokedMethod);560 Method thisMethod = tm.getConstructorOrMethod().getMethod();561 if(confInvocationPassed(tm, tm, testClass, instance)) {562 log(3, "Invoking " + tm.getRealClass().getName() + "." + tm.getMethodName());563 Reporter.setCurrentTestResult(testResult);564 // If this method is a IHookable, invoke its run() method565 IHookable hookableInstance =566 IHookable.class.isAssignableFrom(tm.getRealClass()) ?567 (IHookable) instance : m_configuration.getHookable();568 if (MethodHelper.calculateTimeOut(tm) <= 0) {569 if (hookableInstance != null) {570 MethodInvocationHelper.invokeHookable(instance,571 parameterValues, hookableInstance, thisMethod, testResult);572 } else {573 // Not a IHookable, invoke directly574 MethodInvocationHelper.invokeMethod(thisMethod, instance,575 parameterValues);576 }577 testResult.setStatus(ITestResult.SUCCESS);578 } else {579 // Method with a timeout580 MethodInvocationHelper.invokeWithTimeout(tm, instance, parameterValues, testResult, hookableInstance);581 }582 }583 else {584 testResult.setStatus(ITestResult.SKIP);585 }586 }587 catch(InvocationTargetException ite) {588 testResult.setThrowable(ite.getCause());589 testResult.setStatus(ITestResult.FAILURE);590 }591 catch(ThreadExecutionException tee) { // wrapper for TestNGRuntimeException592 Throwable cause= tee.getCause();593 if(TestNGRuntimeException.class.equals(cause.getClass())) {594 testResult.setThrowable(cause.getCause());595 }596 else {597 testResult.setThrowable(cause);598 }599 testResult.setStatus(ITestResult.FAILURE);600 }601 catch(Throwable thr) { // covers the non-wrapper exceptions602 testResult.setThrowable(thr);603 testResult.setStatus(ITestResult.FAILURE);604 }605 finally {606 // Set end time ASAP607 testResult.setEndMillis(System.currentTimeMillis());608 ExpectedExceptionsHolder expectedExceptionClasses609 = new ExpectedExceptionsHolder(m_annotationFinder, tm, new RegexpExpectedExceptionsHolder(m_annotationFinder, tm));610 List<ITestResult> results = Lists.<ITestResult>newArrayList(testResult);611 handleInvocationResults(tm, results, expectedExceptionClasses, failureContext);612 // If this method has a data provider and just failed, memorize the number613 // at which it failed.614 // Note: we're not exactly testing that this method has a data provider, just615 // that it has parameters, so might have to revisit this if bugs get reported616 // for the case where this method has parameters that don't come from a data617 // provider618 if (testResult.getThrowable() != null && parameterValues.length > 0) {619 tm.addFailedInvocationNumber(parametersIndex);620 }621 //622 // Increment the invocation count for this method623 //624 tm.incrementCurrentInvocationCount();625 // Run invokedMethodListeners after updating TestResult626 runInvokedMethodListeners(AFTER_INVOCATION, invokedMethod, testResult);627 runTestListeners(testResult);628 //629 // Invoke afterMethods only if630 // - lastTimeOnly is not set631 // - lastTimeOnly is set, and we are reaching the last invocationCount632 //633 invokeConfigurations(testClass, tm,634 filterConfigurationMethods(tm, afterMethods, false /* beforeMethods */),635 suite, params, parameterValues,636 instance,637 testResult);638 //639 // Invoke afterGroups configurations640 //641 invokeAfterGroupsConfigurations(testClass, tm, groupMethods, suite,642 params, instance);643 // Reset the test result last. If we do this too early, Reporter.log()644 // invocations from listeners will be discarded645 Reporter.setCurrentTestResult(null);646 }647 return testResult;648 }649 void collectResults(ITestNGMethod testMethod, Collection<ITestResult> results) {650 for (ITestResult result : results) {651 // Collect the results652 final int status = result.getStatus();653 if(ITestResult.SUCCESS == status) {654 m_notifier.addPassedTest(testMethod, result);655 }656 else if(ITestResult.SKIP == status) {657 m_notifier.addSkippedTest(testMethod, result);658 }659 else if(ITestResult.FAILURE == status) {660 m_notifier.addFailedTest(testMethod, result);661 }662 else if(ITestResult.SUCCESS_PERCENTAGE_FAILURE == status) {663 m_notifier.addFailedButWithinSuccessPercentageTest(testMethod, result);664 }665 else {666 assert false : "UNKNOWN STATUS:" + status;667 }668 }669 }670 /**671 * The array of methods contains @BeforeMethods if isBefore if true, @AfterMethods672 * otherwise. This function removes all the methods that should not be run at this673 * point because they are either firstTimeOnly or lastTimeOnly and we haven't reached674 * the current invocationCount yet675 */676 private ITestNGMethod[] filterConfigurationMethods(ITestNGMethod tm,677 ITestNGMethod[] methods, boolean isBefore)678 {679 List<ITestNGMethod> result = Lists.newArrayList();680 for (ITestNGMethod m : methods) {681 ConfigurationMethod cm = (ConfigurationMethod) m;682 if (isBefore) {683 if (! cm.isFirstTimeOnly() ||684 (cm.isFirstTimeOnly() && tm.getCurrentInvocationCount() == 0))685 {686 result.add(m);687 }688 }689 else {690 int current = tm.getCurrentInvocationCount();691 boolean isLast = false;692 // If we have parameters, set the boolean if we are about to run693 // the last invocation694 if (tm.getParameterInvocationCount() > 0) {695 isLast = current == tm.getParameterInvocationCount() * tm.getTotalInvocationCount();696 }697 // If we have invocationCount > 1, set the boolean if we are about to698 // run the last invocation699 else if (tm.getTotalInvocationCount() > 1) {700 isLast = current == tm.getTotalInvocationCount();701 }702 if (! cm.isLastTimeOnly() || (cm.isLastTimeOnly() && isLast)) {703 result.add(m);704 }705 }706 }707 return result.toArray(new ITestNGMethod[result.size()]);708 }709 /**710 * invokeTestMethods() eventually converge here to invoke a single @Test method.711 * <p/>712 * This method is responsible for actually invoking the method. It decides if the invocation713 * must be done:714 * <ul>715 * <li>through an <code>IHookable</code></li>716 * <li>directly (through reflection)</li>717 * <li>in a separate thread (in case it needs to timeout)718 * </ul>719 *720 * <p/>721 * This method is also responsible for invoking @BeforeGroup, @BeforeMethod, @AfterMethod, @AfterGroup722 * if it is the case for the passed in @Test method.723 */724 protected ITestResult invokeTestMethod(Object instance,725 final ITestNGMethod tm,726 Object[] parameterValues,727 int parametersIndex,728 XmlSuite suite,729 Map<String, String> params,730 ITestClass testClass,731 ITestNGMethod[] beforeMethods,732 ITestNGMethod[] afterMethods,733 ConfigurationGroupMethods groupMethods,734 FailureContext failureContext)735 {736 // Mark this method with the current thread id737 tm.setId(ThreadUtil.currentThreadInfo());738 ITestResult result = invokeMethod(instance, tm, parameterValues, parametersIndex, suite, params,739 testClass, beforeMethods, afterMethods, groupMethods,740 failureContext);741 return result;742 }743 /**744 * Filter all the beforeGroups methods and invoke only those that apply745 * to the current test method746 */747 private void invokeBeforeGroupsConfigurations(ITestClass testClass,748 ITestNGMethod currentTestMethod,749 ConfigurationGroupMethods groupMethods,750 XmlSuite suite,751 Map<String, String> params,752 Object instance)753 {754 synchronized(groupMethods) {755 List<ITestNGMethod> filteredMethods = Lists.newArrayList();756 String[] groups = currentTestMethod.getGroups();757 Map<String, List<ITestNGMethod>> beforeGroupMap = groupMethods.getBeforeGroupsMap();758 for (String group : groups) {759 List<ITestNGMethod> methods = beforeGroupMap.get(group);760 if (methods != null) {761 filteredMethods.addAll(methods);762 }763 }764 ITestNGMethod[] beforeMethodsArray = filteredMethods.toArray(new ITestNGMethod[filteredMethods.size()]);765 //766 // Invoke the right groups methods767 //768 if(beforeMethodsArray.length > 0) {769 // don't pass the IClass or the instance as the method may be external770 // the invocation must be similar to @BeforeTest/@BeforeSuite771 invokeConfigurations(null, beforeMethodsArray, suite, params,772 null, /* no parameter values */773 null);774 }775 //776 // Remove them so they don't get run again777 //778 groupMethods.removeBeforeGroups(groups);779 }780 }781 private void invokeAfterGroupsConfigurations(ITestClass testClass,782 ITestNGMethod currentTestMethod,783 ConfigurationGroupMethods groupMethods,784 XmlSuite suite,785 Map<String, String> params,786 Object instance)787 {788 // Skip this if the current method doesn't belong to any group789 // (only a method that belongs to a group can trigger the invocation790 // of afterGroups methods)791 if (currentTestMethod.getGroups().length == 0) {792 return;793 }794 // See if the currentMethod is the last method in any of the groups795 // it belongs to796 Map<String, String> filteredGroups = Maps.newHashMap();797 String[] groups = currentTestMethod.getGroups();798 synchronized(groupMethods) {799 for (String group : groups) {800 if (groupMethods.isLastMethodForGroup(group, currentTestMethod)) {801 filteredGroups.put(group, group);802 }803 }804 if(filteredGroups.isEmpty()) {805 return;806 }807 // The list of afterMethods to run808 Map<ITestNGMethod, ITestNGMethod> afterMethods = Maps.newHashMap();809 // Now filteredGroups contains all the groups for which we need to run the afterGroups810 // method. Find all the methods that correspond to these groups and invoke them.811 Map<String, List<ITestNGMethod>> map = groupMethods.getAfterGroupsMap();812 for (String g : filteredGroups.values()) {813 List<ITestNGMethod> methods = map.get(g);814 // Note: should put them in a map if we want to make sure the same afterGroups815 // doesn't get run twice816 if (methods != null) {817 for (ITestNGMethod m : methods) {818 afterMethods.put(m, m);819 }820 }821 }822 // Got our afterMethods, invoke them823 ITestNGMethod[] afterMethodsArray = afterMethods.keySet().toArray(new ITestNGMethod[afterMethods.size()]);824 // don't pass the IClass or the instance as the method may be external825 // the invocation must be similar to @BeforeTest/@BeforeSuite826 invokeConfigurations(null, afterMethodsArray, suite, params,827 null, /* no parameter values */828 null);829 // Remove the groups so they don't get run again830 groupMethods.removeAfterGroups(filteredGroups.keySet());831 }832 }833 private Object[] getParametersFromIndex(Iterator<Object[]> parametersValues, int index) {834 while (parametersValues.hasNext()) {835 Object[] parameters = parametersValues.next();836 if (index == 0) {837 return parameters;838 }839 index--;840 }841 return null;842 }843 int retryFailed(Object instance,844 final ITestNGMethod tm,845 XmlSuite suite,846 ITestClass testClass,847 ITestNGMethod[] beforeMethods,848 ITestNGMethod[] afterMethods,849 ConfigurationGroupMethods groupMethods,850 List<ITestResult> result,851 int failureCount,852 ExpectedExceptionsHolder expectedExceptionHolder,853 ITestContext testContext,854 Map<String, String> parameters,855 int parametersIndex) {856 final FailureContext failure = new FailureContext();857 failure.count = failureCount;858 do {859 failure.instances = Lists.newArrayList ();860 Map<String, String> allParameters = Maps.newHashMap();861 /**862 * TODO: This recreates all the parameters every time when we only need863 * one specific set. Should optimize it by only recreating the set needed.864 */865 ParameterBag bag = createParameters(tm, parameters,866 allParameters, suite, testContext, null /* fedInstance */);867 Object[] parameterValues =868 getParametersFromIndex(bag.parameterHolder.parameters, parametersIndex);869 result.add(invokeMethod(instance, tm, parameterValues, parametersIndex, suite,870 allParameters, testClass, beforeMethods, afterMethods, groupMethods, failure));871 }872 while (!failure.instances.isEmpty());873 return failure.count;874 }875 private ParameterBag createParameters(ITestNGMethod testMethod,876 Map<String, String> parameters,877 Map<String, String> allParameterNames,878 XmlSuite suite,879 ITestContext testContext,880 Object fedInstance)881 {882 Object instance;883 if (fedInstance != null) {884 instance = fedInstance;885 }886 else {887 instance = testMethod.getInstance();888 }889 ParameterBag bag = handleParameters(testMethod,890 instance, allParameterNames, parameters, null, suite, testContext, fedInstance, null);891 return bag;892 }893 /**894 * Invoke all the test methods. Note the plural: the method passed in895 * parameter might be invoked several times if the test class it belongs896 * to has more than one instance (i.e., if an @Factory method has been897 * declared somewhere that returns several instances of this TestClass).898 * If no @Factory method was specified, testMethod will only be invoked899 * once.900 * <p/>901 * Note that this method also takes care of invoking the beforeTestMethod902 * and afterTestMethod, if any.903 *904 * Note (alex): this method can be refactored to use a SingleTestMethodWorker that905 * directly invokes906 * {@link #invokeTestMethod(Object, ITestNGMethod, Object[], int, XmlSuite, Map, ITestClass, ITestNGMethod[], ITestNGMethod[], ConfigurationGroupMethods, FailureContext)}907 * and this would simplify the implementation (see how DataTestMethodWorker is used)908 */909 @Override910 public List<ITestResult> invokeTestMethods(ITestNGMethod testMethod,911 XmlSuite suite,912 Map<String, String> testParameters,913 ConfigurationGroupMethods groupMethods,914 Object instance,915 ITestContext testContext)916 {917 // Potential bug here if the test method was declared on a parent class918 assert null != testMethod.getTestClass()919 : "COULDN'T FIND TESTCLASS FOR " + testMethod.getRealClass();920 if (!MethodHelper.isEnabled(testMethod.getMethod(), m_annotationFinder)) {921 // return if the method is not enabled. No need to do any more calculations922 return Collections.emptyList();923 }924 // By the time this testMethod to be invoked,925 // all dependencies should be already run or we need to skip this method,926 // so invocation count should not affect dependencies check927 final String okToProceed = checkDependencies(testMethod, testContext.getAllTestMethods());928 if (okToProceed != null) {929 //930 // Not okToProceed. Test is being skipped931 //932 ITestResult result = registerSkippedTestResult(testMethod, null, System.currentTimeMillis(),933 new Throwable(okToProceed));...

Full Screen

Full Screen

Source:BaseUnitTest.java Github

copy

Full Screen

...208 when(constructorOrMethod.getMethod()).thenReturn(method);209 ITestNGMethod iTestNGMethod = mock(ITestNGMethod.class);210 when(iTestNGMethod.getConstructorOrMethod()).thenReturn(constructorOrMethod);211 when(iTestNGMethod.getMethodName()).thenReturn(methodName);212 when(iTestNGMethod.getRealClass()).thenReturn(clazz);213 when(iTestNGMethod.isTest()).thenReturn(true);214 when(iTestNGMethod.getInvocationCount()).thenReturn(1);215 return iTestNGMethod;216 }217 @SuppressWarnings("unchecked")218 protected static ITestClass getMockITestClass(Class aClass) {219 ITestClass iTestClass = mock(ITestClass.class);220 when(iTestClass.getRealClass()).thenReturn(aClass);221 return iTestClass;222 }223 protected static ITestResult getMockITestResult(Integer status) {224 Method method;225 try {226 method = TestNGTestClassWithSuite.class.getMethod("iTestResultMethodWithDetails");227 } catch (NoSuchMethodException e) {228 throw new RuntimeException(e);229 }230 ConstructorOrMethod constructorOrMethod = mock(ConstructorOrMethod.class);231 when(constructorOrMethod.getMethod()).thenReturn(method);232 ITestNGMethod iTestNGMethod = mock(ITestNGMethod.class);233 when(iTestNGMethod.getConstructorOrMethod()).thenReturn(constructorOrMethod);234 ITestResult iTestResult = mock(ITestResult.class);...

Full Screen

Full Screen

Source:MethodInheritance.java Github

copy

Full Screen

...37 private static final Comparator<ITestNGMethod> COMPARATOR = new Comparator<ITestNGMethod>() {38 @Override39 public int compare(ITestNGMethod o1, ITestNGMethod o2) {40 int result = -2;41 Class<?> thisClass = o1.getRealClass();42 Class<?> otherClass = o2.getRealClass();43 if (thisClass.isAssignableFrom(otherClass)) {44 result = -1;45 } else if (otherClass.isAssignableFrom(thisClass)) {46 result = 1;47 } else if (o1.equals(o2)) {48 result = 0;49 }50 return result;51 }52 };53 /**54 * Look in map for a class that is a superclass of methodClass55 */56 private static List<ITestNGMethod> findMethodListSuperClass(Map<Class, List<ITestNGMethod>> map,57 Class< ? extends ITestNGMethod> methodClass)58 {59 for (Map.Entry<Class, List<ITestNGMethod>> entry : map.entrySet()) {60 if (entry.getKey().isAssignableFrom(methodClass)) {61 return entry.getValue();62 }63 }64 return null;65 }66 /**67 * Look in map for a class that is a subclass of methodClass68 */69 private static Class findSubClass(Map<Class, List<ITestNGMethod>> map,70 Class< ? extends ITestNGMethod> methodClass)71 {72 for (Class cls : map.keySet()) {73 if (methodClass.isAssignableFrom(cls)) {74 return cls;75 }76 }77 return null;78 }79 /**80 * Fix the methodsDependedUpon to make sure that @Configuration methods81 * respect inheritance (before methods are invoked in the order Base first82 * and after methods are invoked in the order Child first)83 *84 * @param methods the list of methods85 * @param before true if we are handling a before method (meaning, the methods86 * need to be sorted base class first and subclass last). false otherwise (subclass87 * methods first, base classes last).88 */89 public static void fixMethodInheritance(ITestNGMethod[] methods, boolean before) {90 // Map of classes -> List of methods that belong to this class or same hierarchy91 Map<Class, List<ITestNGMethod>> map = Maps.newHashMap();92 //93 // Put the list of methods in their hierarchy buckets94 //95 for (ITestNGMethod method : methods) {96 Class< ? extends ITestNGMethod> methodClass = method.getRealClass();97 List<ITestNGMethod> l = findMethodListSuperClass(map, methodClass);98 if (null != l) {99 l.add(method);100 }101 else {102 Class subClass = findSubClass(map, methodClass);103 if (null != subClass) {104 l = map.get(subClass);105 l.add(method);106 map.remove(subClass);107 map.put(methodClass, l);108 }109 else {110 l = Lists.newArrayList();111 l.add(method);112 map.put(methodClass, l);113 }114 }115 }116 //117 // Each bucket that has a list bigger than one element gets sorted118 //119 for (List<ITestNGMethod> l : map.values()) {120 if (l.size() > 1) {121 // Sort them122 sortMethodsByInheritance(l, before);123 /*124 * Set methodDependedUpon accordingly125 * E.g. Base class can have multiple @BeforeClass methods. Need to ensure126 * that @BeforeClass methods in derived class depend on all @BeforeClass methods127 * of base class. Vice versa for @AfterXXX methods128 */129 for (int i = 0; i < l.size() - 1; i++) {130 ITestNGMethod m1 = l.get(i);131 for (int j = i + 1; j < l.size(); j++) {132 ITestNGMethod m2 = l.get(j);133 if (!equalsEffectiveClass(m1, m2) && !dependencyExists(m1, m2, methods)) {134 Utils.log("MethodInheritance", 4, m2 + " DEPENDS ON " + m1);135 m2.addMethodDependedUpon(MethodHelper.calculateMethodCanonicalName(m1));136 }137 }138 }139 }140 }141 }142 private static boolean dependencyExists(ITestNGMethod m1, ITestNGMethod m2, ITestNGMethod[] methods) {143 return internalDependencyExists(m1, m2, methods) || internalDependencyExists(m2, m1, methods);144 }145 private static boolean internalDependencyExists(ITestNGMethod m1, ITestNGMethod m2, ITestNGMethod[] methods) {146 ITestNGMethod[] methodsNamed =147 MethodHelper.findDependedUponMethods(m1, methods);148 for (ITestNGMethod method : methodsNamed) {149 if (method.equals(m2)) {150 return true;151 }152 }153 for (String group : m1.getGroupsDependedUpon()) {154 ITestNGMethod[] methodsThatBelongToGroup =155 MethodGroupsHelper.findMethodsThatBelongToGroup(m1, methods, group);156 for (ITestNGMethod method : methodsThatBelongToGroup) {157 if (method.equals(m2)) {158 return true;159 }160 }161 }162 return false;163 }164 private static boolean equalsEffectiveClass(ITestNGMethod m1, ITestNGMethod m2) {165 try {166 Class c1 = m1.getRealClass();167 Class c2 = m2.getRealClass();168 return c1 == null ? c2 == null : c1.equals(c2);169 }170 catch(Exception ex) {171 return false;172 }173 }174 /**175 * Given a list of methods belonging to the same class hierarchy, orders them176 * from the base class to the child (if true) or from child to base class (if false)177 * @param methods178 */179 private static void sortMethodsByInheritance(List<ITestNGMethod> methods,180 boolean baseClassToChild)181 {...

Full Screen

Full Screen

Source:ContextInitializingListenerTest.java Github

copy

Full Screen

...58 setField(listener, "contextInitializer", MockContext.class);59 logger.info("Attempting to initialize shared context");60 ITestContext mockContext = mock(ITestContext.class, withSettings().stubOnly());61 ITestNGMethod mockMethod = mock(ITestNGMethod.class);62 when(mockMethod.getRealClass()).thenReturn(MockTestClass.class);63 when(mockContext.getAllTestMethods()).thenReturn(new ITestNGMethod[]{mockMethod});64 listener.onStart(mockContext);65 logger.info("Context is present now");66 /*ContextInitializingListener can't exit without functional context available*/67 Assert.assertNotNull(AppContextHolder.getContext());68 }, pool))69 .toArray(CompletableFuture[]::new))70 .get();71 /*Application context is expected to be initialized once*/72 verify(spyHolder).init();73 /*Environment is expected to be initialized for each test execution*/74 verify(mockEnvInitializer, times(threadNum)).initializeEnvironment(any(Properties.class));75 }76 @Configuration...

Full Screen

Full Screen

Source:Listener.java Github

copy

Full Screen

...194 // This will return method names to the calling function195196 private String returnMethodName(ITestNGMethod method) {197198 return method.getRealClass().getSimpleName() + "." + method.getMethodName();199200 }201202} ...

Full Screen

Full Screen

Source:NoOpTestClass.java Github

copy

Full Screen

...36 protected NoOpTestClass() {37 }3839 public NoOpTestClass(ITestClass testClass) {40 m_testClass= testClass.getRealClass();41 m_testName= testClass.getName();42 m_beforeSuiteMethods= testClass.getBeforeSuiteMethods();43 m_beforeTestConfMethods= testClass.getBeforeTestConfigurationMethods();44 m_beforeGroupsMethods= testClass.getBeforeGroupsMethods();45 m_beforeClassMethods= testClass.getBeforeClassMethods();46 m_beforeTestMethods= testClass.getBeforeTestMethods();47 m_afterSuiteMethods= testClass.getAfterSuiteMethods();48 m_afterTestConfMethods= testClass.getAfterTestConfigurationMethods();49 m_afterGroupsMethods= testClass.getAfterGroupsMethods();50 m_afterClassMethods= testClass.getAfterClassMethods();51 m_afterTestMethods= testClass.getAfterTestMethods();52 m_instances= testClass.getInstances(true);53 m_instanceHashes= testClass.getInstanceHashCodes();54 }5556 public void setBeforeTestMethods(ITestNGMethod[] beforeTestMethods) {57 m_beforeTestMethods= beforeTestMethods;58 }5960 public void setAfterTestMethod(ITestNGMethod[] afterTestMethods) {61 m_afterTestMethods= afterTestMethods;62 }6364 /**65 * @return Returns the afterClassMethods.66 */67 public ITestNGMethod[] getAfterClassMethods() {68 return m_afterClassMethods;69 }7071 /**72 * @return Returns the afterTestMethods.73 */74 public ITestNGMethod[] getAfterTestMethods() {75 return m_afterTestMethods;76 }7778 /**79 * @return Returns the beforeClassMethods.80 */81 public ITestNGMethod[] getBeforeClassMethods() {82 return m_beforeClassMethods;83 }8485 /**86 * @return Returns the beforeTestMethods.87 */88 public ITestNGMethod[] getBeforeTestMethods() {89 return m_beforeTestMethods;90 }9192 /**93 * @return Returns the testMethods.94 */95 public ITestNGMethod[] getTestMethods() {96 return m_testMethods;97 }9899 public ITestNGMethod[] getBeforeSuiteMethods() {100 return m_beforeSuiteMethods;101 }102103 public ITestNGMethod[] getAfterSuiteMethods() {104 return m_afterSuiteMethods;105 }106107 public ITestNGMethod[] getBeforeTestConfigurationMethods() {108 return m_beforeTestConfMethods;109 }110111 public ITestNGMethod[] getAfterTestConfigurationMethods() {112 return m_afterTestConfMethods;113 }114115 /**116 * @return all @Configuration methods that should be invoked before certain groups117 */118 public ITestNGMethod[] getBeforeGroupsMethods() {119 return m_beforeGroupsMethods;120 }121122 /**123 * @return all @Configuration methods that should be invoked after certain groups124 */125 public ITestNGMethod[] getAfterGroupsMethods() {126 return m_afterGroupsMethods;127 }128129 /**130 * @see org.testng.ITestClass#getInstanceCount()131 */132 public int getInstanceCount() {133 return m_instances.length;134 }135136 /**137 * @see org.testng.ITestClass#getInstanceHashCodes()138 */139 public long[] getInstanceHashCodes() {140 return m_instanceHashes;141 }142143 /**144 * @see org.testng.ITestClass#getInstances(boolean)145 */146 public Object[] getInstances(boolean reuse) {147 return m_instances;148 }149150 public String getName() {151 return m_testClass.getName();152 }153154 public Class getRealClass() {155 return m_testClass;156 }157158 /**159 * @see org.testng.IClass#addInstance(java.lang.Object)160 */161 public void addInstance(Object instance) {162 }163164 public void setTestClass(Class< ? > declaringClass) {165 m_testClass = declaringClass;166 }167}

Full Screen

Full Screen

getRealClass

Using AI Code Generation

copy

Full Screen

1ITestNGMethod[] methods = testResult.getTestClass().getTestMethods();2for (ITestNGMethod method : methods) {3 String realClassName = method.getRealClass().getName();4 System.out.println(realClassName);5}6ITestNGMethod method = testResult.getMethod();7String realClassName = method.getRealClass().getName();8System.out.println(realClassName);9ITestNGMethod[] methods = testResult.getTestClass().getTestMethods();10for (ITestNGMethod method : methods) {11 String realClassName = method.getRealClass().getName();12 System.out.println(realClassName);13}14ITestNGMethod method = testResult.getMethod();15String realClassName = method.getRealClass().getName();16System.out.println(realClassName);17ITestNGMethod[] methods = testResult.getTestClass().getTestMethods();18for (ITestNGMethod method : methods) {19 String realClassName = method.getRealClass().getName();20 System.out.println(realClassName);21}22ITestNGMethod method = testResult.getMethod();23String realClassName = method.getRealClass().getName();24System.out.println(realClassName);25ITestNGMethod[] methods = testResult.getTestClass().getTestMethods();26for (ITestNGMethod method : methods) {27 String realClassName = method.getRealClass().getName();28 System.out.println(realClassName);29}

Full Screen

Full Screen

getRealClass

Using AI Code Generation

copy

Full Screen

1package com.automationintesting.api;2import org.testng.ITestNGMethod;3public class TestNGMethod {4 public static void main(String[] args) {5 ITestNGMethod[] allTestMethods = new TestNGMethod().getAllTestMethods();6 for(ITestNGMethod testMethod : allTestMethods){7 System.out.println(testMethod.getRealClass().getName());8 }9 }10 public ITestNGMethod[] getAllTestMethods(){11 return null;12 }13}

Full Screen

Full Screen

TestNG tutorial

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.

Chapters

  1. JUnit 5 vs. TestNG: Compare and explore the core differences between JUnit 5 and TestNG from the Selenium WebDriver viewpoint.
  2. Installing TestNG in Eclipse: Start installing the TestNG Plugin and learn how to set up TestNG in Eclipse to begin constructing a framework for your test project.
  3. Create TestNG Project in Eclipse: Get started with creating a TestNG project and write your first TestNG test script.
  4. Automation using TestNG: Dive into how to install TestNG in this Selenium TestNG tutorial, the fundamentals of developing an automation script for Selenium automation testing.
  5. Parallel Test Execution in TestNG: Here are some essential elements of parallel testing with TestNG in this Selenium TestNG tutorial.
  6. Creating TestNG XML File: Here is a step-by-step tutorial on creating a TestNG XML file to learn why and how it is created and discover how to run the TestNG XML file being executed in parallel.
  7. Automation with Selenium, Cucumber & TestNG: Explore for an in-depth tutorial on automation using Selenium, Cucumber, and TestNG, as TestNG offers simpler settings and more features.
  8. JUnit Selenium Tests using TestNG: Start running your regular and parallel tests by looking at how to run test cases in Selenium using JUnit and TestNG without having to rewrite the tests.
  9. Group Test Cases in TestNG: Along with the explanation and demonstration using relevant TestNG group examples, learn how to group test cases in TestNG.
  10. Prioritizing Tests in TestNG: Get started with how to prioritize test cases in TestNG for Selenium automation testing.
  11. Assertions in TestNG: Examine what TestNG assertions are, the various types of TestNG assertions, and situations that relate to Selenium automated testing.
  12. DataProviders in TestNG: Deep dive into learning more about TestNG's DataProvider and how to effectively use it in our test scripts for Selenium test automation.
  13. Parameterization in TestNG: Here are the several parameterization strategies used in TestNG tests and how to apply them in Selenium automation scripts.
  14. TestNG Listeners in Selenium WebDriver: Understand the various TestNG listeners to utilize them effectively for your next plan when working with TestNG and Selenium automation.
  15. TestNG Annotations: Learn more about the execution order and annotation attributes, and refer to the prerequisites required to set up TestNG.
  16. TestNG Reporter Log in Selenium: Find out how to use the TestNG Reporter Log and learn how to eliminate the need for external software with TestNG Reporter Class to boost productivity.
  17. TestNG Reports in Jenkins: Discover how to generate TestNG reports in Jenkins if you want to know how to create, install, and share TestNG reports in Jenkins.

Certification

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.

YouTube

Watch this complete tutorial to learn how you can leverage the capabilities of the TestNG framework for Selenium automation testing.

Run Testng automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful