/** * Process the {@link LinkedListeners} annotation of the specified test class. * * @param testClass test class */ private void processLinkedListeners(Class<?> testClass) { Objects.requireNonNull(testClass, "[testClass] must be non-null"); LinkedListeners annotation = testClass.getAnnotation(LinkedListeners.class); if (null != annotation) { Class<?> markedClass = testClass; while (null == markedClass.getDeclaredAnnotation(LinkedListeners.class)) { markedClass = markedClass.getSuperclass(); } if ( ! markedClasses.contains(markedClass)) { markedClasses.add(markedClass); for (Class<? extends ITestNGListener> listener : annotation.value()) { attachListener(listener, null); } } } }
@Test public void verifyHappyPath() { ListenerChain lc = new ListenerChain(); TestListenerAdapter tla = new TestListenerAdapter(); TestNG testNG = new TestNG(); testNG.setTestClasses(new Class[]{ArtifactCollectorTestCases.class}); testNG.addListener((ITestNGListener) lc); testNG.addListener((ITestNGListener) tla); testNG.setGroups("testPassed"); testNG.run(); assertEquals(tla.getPassedTests().size(), 1, "Incorrect passed test count"); assertEquals(tla.getFailedTests().size(), 0, "Incorrect failed test count"); assertEquals(tla.getSkippedTests().size(), 0, "Incorrect skipped test count"); assertEquals(tla.getFailedButWithinSuccessPercentageTests().size(), 0, "Incorrect curve-graded success count"); assertEquals(tla.getConfigurationFailures().size(), 0, "Incorrect configuration method failure count"); assertEquals(tla.getConfigurationSkips().size(), 0, "Incorrect configuration method skip count"); ITestResult result = tla.getPassedTests().get(0); assertNull(UnitTestArtifact.getCaptureState(result), "Artifact provider capture state should be 'null'"); assertNull(UnitTestCapture.getArtifactPath(result), "Artifact capture should not have been requested"); }
@Test public void verifyCaptureOnFailure() { ListenerChain lc = new ListenerChain(); TestListenerAdapter tla = new TestListenerAdapter(); TestNG testNG = new TestNG(); testNG.setTestClasses(new Class[]{ArtifactCollectorTestCases.class}); testNG.addListener((ITestNGListener) lc); testNG.addListener((ITestNGListener) tla); testNG.setGroups("testFailed"); testNG.run(); assertEquals(tla.getPassedTests().size(), 0, "Incorrect passed test count"); assertEquals(tla.getFailedTests().size(), 1, "Incorrect failed test count"); assertEquals(tla.getSkippedTests().size(), 0, "Incorrect skipped test count"); assertEquals(tla.getFailedButWithinSuccessPercentageTests().size(), 0, "Incorrect curve-graded success count"); assertEquals(tla.getConfigurationFailures().size(), 0, "Incorrect configuration method failure count"); assertEquals(tla.getConfigurationSkips().size(), 0, "Incorrect configuration method skip count"); ITestResult result = tla.getFailedTests().get(0); assertEquals(UnitTestArtifact.getCaptureState(result), CaptureState.CAPTURE_SUCCESS, "Incorrect artifact provider capture state"); assertTrue(UnitTestCapture.getArtifactPath(result).isPresent(), "Artifact capture output path is not present"); }
@Test public void verifyCanNotCapture() { ListenerChain lc = new ListenerChain(); TestListenerAdapter tla = new TestListenerAdapter(); TestNG testNG = new TestNG(); testNG.setTestClasses(new Class[]{ArtifactCollectorTestCases.class}); testNG.addListener((ITestNGListener) lc); testNG.addListener((ITestNGListener) tla); testNG.setGroups("canNotCapture"); testNG.run(); assertEquals(tla.getPassedTests().size(), 0, "Incorrect passed test count"); assertEquals(tla.getFailedTests().size(), 1, "Incorrect failed test count"); assertEquals(tla.getSkippedTests().size(), 0, "Incorrect skipped test count"); assertEquals(tla.getFailedButWithinSuccessPercentageTests().size(), 0, "Incorrect curve-graded success count"); assertEquals(tla.getConfigurationFailures().size(), 0, "Incorrect configuration method failure count"); assertEquals(tla.getConfigurationSkips().size(), 0, "Incorrect configuration method skip count"); ITestResult result = tla.getFailedTests().get(0); assertEquals(UnitTestArtifact.getCaptureState(result), CaptureState.CAN_NOT_CAPTURE, "Incorrect artifact provider capture state"); assertFalse(UnitTestCapture.getArtifactPath(result).isPresent(), "Artifact capture output path should not be present"); }
@Test public void verifyWillNotCapture() { ListenerChain lc = new ListenerChain(); TestListenerAdapter tla = new TestListenerAdapter(); TestNG testNG = new TestNG(); testNG.setTestClasses(new Class[]{ArtifactCollectorTestCases.class}); testNG.addListener((ITestNGListener) lc); testNG.addListener((ITestNGListener) tla); testNG.setGroups("willNotCapture"); testNG.run(); assertEquals(tla.getPassedTests().size(), 0, "Incorrect passed test count"); assertEquals(tla.getFailedTests().size(), 1, "Incorrect failed test count"); assertEquals(tla.getSkippedTests().size(), 0, "Incorrect skipped test count"); assertEquals(tla.getFailedButWithinSuccessPercentageTests().size(), 0, "Incorrect curve-graded success count"); assertEquals(tla.getConfigurationFailures().size(), 0, "Incorrect configuration method failure count"); assertEquals(tla.getConfigurationSkips().size(), 0, "Incorrect configuration method skip count"); ITestResult result = tla.getFailedTests().get(0); assertEquals(UnitTestArtifact.getCaptureState(result), CaptureState.CAPTURE_FAILED, "Incorrect artifact provider capture state"); assertFalse(UnitTestCapture.getArtifactPath(result).isPresent(), "Artifact capture output path should not be present"); }
@Test public void verifyOnDemandCapture() { ListenerChain lc = new ListenerChain(); TestListenerAdapter tla = new TestListenerAdapter(); TestNG testNG = new TestNG(); testNG.setTestClasses(new Class[]{ArtifactCollectorTestCases.class}); testNG.addListener((ITestNGListener) lc); testNG.addListener((ITestNGListener) tla); testNG.setGroups("onDemandCapture"); testNG.run(); assertEquals(tla.getPassedTests().size(), 1, "Incorrect passed test count"); assertEquals(tla.getFailedTests().size(), 0, "Incorrect failed test count"); assertEquals(tla.getSkippedTests().size(), 0, "Incorrect skipped test count"); assertEquals(tla.getFailedButWithinSuccessPercentageTests().size(), 0, "Incorrect curve-graded success count"); assertEquals(tla.getConfigurationFailures().size(), 0, "Incorrect configuration method failure count"); assertEquals(tla.getConfigurationSkips().size(), 0, "Incorrect configuration method skip count"); ITestResult result = tla.getPassedTests().get(0); assertEquals(UnitTestArtifact.getCaptureState(result), CaptureState.CAPTURE_SUCCESS, "Incorrect artifact provider capture state"); assertTrue(UnitTestCapture.getArtifactPath(result).isPresent(), "Artifact capture output path is not present"); }
@Test public void testAttributeHandOff() { ExecutionFlowController efc = new ExecutionFlowController(); TestListenerAdapter tla = new TestListenerAdapter(); TestNG testNG = new TestNG(); testNG.setTestClasses(new Class[]{HappyPathClass.class}); testNG.addListener((ITestNGListener) efc); testNG.addListener((ITestNGListener) tla); testNG.run(); assertEquals(tla.getFailedTests().size(), 0, "Unexpected test method failure"); assertEquals(tla.getConfigurationFailures().size(), 0, "Unexpected configuration method failure"); assertEquals(tla.getPassedTests().size(), 2, "Incorrect passed test count"); assertEquals(tla.getFailedTests().size(), 0, "Incorrect failed test count"); assertEquals(tla.getSkippedTests().size(), 0, "Incorrect skipped test count"); assertEquals(HappyPathClass.fromBefore, HappyPathClass.VALUE, "Incorrect [before] value"); assertEquals(HappyPathClass.fromMethod, HappyPathClass.VALUE, "Incorrect [method] value"); assertEquals(HappyPathClass.fromAfter, HappyPathClass.VALUE, "Incorrect [after] value"); }
@Test public void testSkipFromMethod() { ExecutionFlowController efc = new ExecutionFlowController(); TestListenerAdapter tla = new TestListenerAdapter(); TestNG testNG = new TestNG(); testNG.setTestClasses(new Class[]{SkipFromMethod.class}); testNG.addListener((ITestNGListener) efc); testNG.addListener((ITestNGListener) tla); testNG.run(); assertEquals(tla.getFailedTests().size(), 0, "Unexpected test method failure"); assertEquals(tla.getConfigurationFailures().size(), 0, "Unexpected configuration method failure"); assertEquals(tla.getPassedTests().size(), 1, "Incorrect passed test count"); assertEquals(tla.getFailedTests().size(), 0, "Incorrect failed test count"); assertEquals(tla.getSkippedTests().size(), 1, "Incorrect skipped test count"); assertEquals(tla.getSkippedTests().get(0).getName(), "testMethod", "Incorrect skipped test name"); assertEquals(SkipFromMethod.fromBefore, SkipFromMethod.VALUE, "Incorrect [before] value"); assertEquals(SkipFromMethod.fromMethod, SkipFromMethod.VALUE, "Incorrect [method] value"); assertEquals(SkipFromMethod.fromAfter, SkipFromMethod.VALUE, "Incorrect [after] value"); }
private boolean isAnnotated(Class<?> realClass) { Listeners listeners = realClass.getAnnotation(Listeners.class); if (listeners == null) { return false; } for (Class<? extends ITestNGListener> listenerClass : listeners.value()) { if (listenerClass.equals(MockitoTestNgListener.class)) { return true; } } return false; }
/** * Get the listener of the specified type that's attached to the listener chain. * * @param <T> listener type * @param listenerType listener type * @return listener of the specified type */ public static <T extends ITestNGListener> T getLinkedListener(Class<T> listenerType) { ITestResult testResult = Reporter.getCurrentTestResult(); Optional<T> optListener = ListenerChain.getAttachedListener(testResult, listenerType); if (optListener.isPresent()) { return optListener.get(); } throw new IllegalStateException(listenerType.getSimpleName() + " listener wasn't found on the listener chain"); }
private InvokedMethodNameListener run(final String suitePath, final Supplier<BeforeMethodListener> listenerSupplier) { final TestNG tng = create(); final InvokedMethodNameListener nameListener = new InvokedMethodNameListener(); final BeforeMethodListener spyListener = listenerSupplier.get(); tng.addListener((ITestNGListener) nameListener); tng.addListener((ITestNGListener) spyListener); tng.setTestSuites(singletonList(suitePath)); tng.run(); return nameListener; }
/** * Constructs new {@link TestNGEngine} object with wanted listeners. * * @param listeners an instance (or instances) of engine's listeners * @return an instance of {@link TestNGEngine} */ @Override public TestNGEngine with(ITestNGListener... listeners) { final TestNG testNG = new TestNG(false); Arrays.stream(listeners).forEach(testNG::addListener); return new TestNGEngine(testNG, this.tests); }
/** * Constructs new {@link TestNGKernel} object with wanted listeners. * * @param listeners an instance (or instances) of engine's listeners * @return an instance of {@link TestNGKernel} */ @Override public TestNGKernel with(ITestNGListener... listeners) { final TestNGKernel kernel = new TestNGKernel(this.suite); Arrays.stream(listeners).forEach(kernel.engine::addListener); return kernel; }
@BeforeMethod(description = "Configure TestNG engine") public void prepare() { results = new AllureResultsWriterStub(); lifecycle = new AllureLifecycle(results); AllureTestNg adapter = new AllureTestNg(lifecycle); testNg = new TestNG(false); testNg.addListener((ITestNGListener) adapter); }
/** * Get reference to an instance of the specified listener type. * * @param <T> listener type * @param suite TestNG suite object * @param listenerType listener type * @return optional listener instance */ public static <T extends ITestNGListener> Optional<T> getAttachedListener(ISuite suite, Class<T> listenerType) { Objects.requireNonNull(suite, "[suite] must be non-null"); Objects.requireNonNull(listenerType, "[listenerType] must be non-null"); ListenerChain chain = (ListenerChain) suite.getAttribute(LISTENER_CHAIN); Objects.requireNonNull(chain, "Specified suite has no ListenerChain"); return chain.getAttachedListener(listenerType); }
/** * Get reference to an instance of the specified listener type. * * @param <T> listener type * @param listenerType listener type * @return optional listener instance */ @SuppressWarnings("unchecked") public <T extends ITestNGListener> Optional<T> getAttachedListener(Class<T> listenerType) { for (ITestNGListener listener : listeners) { if (listener.getClass() == listenerType) { return Optional.of((T) listener); } } return Optional.empty(); }
@Test public void testSkipFromBefore() { ExecutionFlowController efc = new ExecutionFlowController(); TestListenerAdapter tla = new TestListenerAdapter(); TestNG testNG = new TestNG(); testNG.setTestClasses(new Class[]{SkipFromBefore.class}); testNG.addListener((ITestNGListener) efc); testNG.addListener((ITestNGListener) tla); testNG.setConfigFailurePolicy(FailurePolicy.CONTINUE); testNG.run(); assertEquals(tla.getFailedTests().size(), 0, "Unexpected test method failure"); assertEquals(tla.getConfigurationFailures().size(), 0, "Unexpected configuration method failure"); assertEquals(tla.getPassedTests().size(), 1, "Incorrect passed test count"); assertEquals(tla.getFailedTests().size(), 0, "Incorrect failed test count"); assertEquals(tla.getConfigurationSkips().size(), 1, "Incorrect configuration skip count"); assertEquals(tla.getSkippedTests().size(), 1, "Incorrect skipped test count"); ITestResult methodResult = tla.getSkippedTests().get(0); assertEquals(methodResult.getName(), "testMethod", "Incorrect skipped test name"); assertEquals(SkipFromBefore.fromBefore, SkipFromBefore.VALUE, "Incorrect [before] value"); assertEquals(methodResult.getAttribute(SkipFromBefore.ATTRIBUTE), SkipFromBefore.VALUE, "Incorrect [method] value"); assertEquals(SkipFromBefore.fromAfter, SkipFromBefore.VALUE, "Incorrect [after] value"); }
@Test public void testMethodListenerExtension() { ExecutionFlowController efc = new ExecutionFlowController(); TestListenerAdapter tla = new TestListenerAdapter(); TestNG testNG = new TestNG(); testNG.setTestClasses(new Class[]{MethodListenerExtension.class}); testNG.addListener((ITestNGListener) efc); testNG.addListener((ITestNGListener) tla); testNG.run(); assertEquals(tla.getFailedTests().size(), 0, "Unexpected test method failure"); assertEquals(tla.getConfigurationFailures().size(), 0, "Unexpected configuration method failure"); assertEquals(tla.getPassedTests().size(), 1, "Incorrect passed test count"); assertEquals(tla.getFailedTests().size(), 0, "Incorrect failed test count"); assertEquals(tla.getSkippedTests().size(), 0, "Incorrect skipped test count"); assertTrue(MethodListenerExtension.beforeMethodBefore, "Incorrect [beforeMethod] 'before' value"); assertTrue(MethodListenerExtension.testMethodBefore, "Incorrect [testMethod] 'before' value"); assertTrue(MethodListenerExtension.afterMethodBefore, "Incorrect [afterMethod] 'before' value"); assertTrue(MethodListenerExtension.beforeMethodAfter, "Incorrect [beforeMethod] 'after' value"); assertTrue(MethodListenerExtension.testMethodAfter, "Incorrect [testMethod] 'after' value"); assertTrue(MethodListenerExtension.afterMethodAfter, "Incorrect [afterMethod] 'after' value"); }
@Test public void verifyTestFailed() { ListenerChain lc = new ListenerChain(); TestListenerAdapter tla = new TestListenerAdapter(); TestNG testNG = new TestNG(); testNG.setTestClasses(new Class[]{ListenerChainTestCases.class}); testNG.addListener((ITestNGListener) lc); testNG.addListener((ITestNGListener) tla); testNG.setGroups("testFailed"); testNG.run(); ChainedListener chainedListener = lc.getAttachedListener(ChainedListener.class).get(); assertEquals(tla.getPassedTests().size(), 0, "Incorrect passed test count"); assertEquals(tla.getFailedTests().size(), 1, "Incorrect failed test count"); assertEquals(tla.getSkippedTests().size(), 0, "Incorrect skipped test count"); assertEquals(tla.getFailedButWithinSuccessPercentageTests().size(), 0, "Incorrect curve-graded success count"); assertEquals(tla.getConfigurationFailures().size(), 0, "Incorrect configuration method failure count"); assertEquals(tla.getConfigurationSkips().size(), 0, "Incorrect configuration method skip count"); assertTrue(chainedListener.configSuccess.contains("beforeSuccess")); assertTrue(chainedListener.configSuccess.contains("afterSuccess")); assertTrue(chainedListener.beforeConfig.contains("beforeSuccess")); assertTrue(chainedListener.beforeConfig.contains("afterSuccess")); assertTrue(chainedListener.beforeMethodBefore.contains("beforeSuccess")); assertTrue(chainedListener.beforeMethodAfter.contains("beforeSuccess")); assertTrue(chainedListener.testMethodBefore.contains("testFailed")); assertTrue(chainedListener.testMethodAfter.contains("testFailed")); assertTrue(chainedListener.afterMethodBefore.contains("afterSuccess")); assertTrue(chainedListener.afterMethodAfter.contains("afterSuccess")); assertTrue(chainedListener.testStarted.contains("testFailed")); assertTrue(chainedListener.testFailure.contains("testFailed")); }
@Test public void verifyTestSkipped() { ListenerChain lc = new ListenerChain(); TestListenerAdapter tla = new TestListenerAdapter(); TestNG testNG = new TestNG(); testNG.setTestClasses(new Class[]{ListenerChainTestCases.class}); testNG.addListener((ITestNGListener) lc); testNG.addListener((ITestNGListener) tla); testNG.setGroups("testSkipped"); testNG.run(); ChainedListener chainedListener = lc.getAttachedListener(ChainedListener.class).get(); assertEquals(tla.getPassedTests().size(), 0, "Incorrect passed test count"); assertEquals(tla.getFailedTests().size(), 0, "Incorrect failed test count"); assertEquals(tla.getSkippedTests().size(), 1, "Incorrect skipped test count"); assertEquals(tla.getFailedButWithinSuccessPercentageTests().size(), 0, "Incorrect curve-graded success count"); assertEquals(tla.getConfigurationFailures().size(), 0, "Incorrect configuration method failure count"); assertEquals(tla.getConfigurationSkips().size(), 0, "Incorrect configuration method skip count"); assertTrue(chainedListener.configSuccess.contains("beforeSuccess")); assertTrue(chainedListener.configSuccess.contains("afterSuccess")); assertTrue(chainedListener.beforeConfig.contains("beforeSuccess")); assertTrue(chainedListener.beforeConfig.contains("afterSuccess")); assertTrue(chainedListener.beforeMethodBefore.contains("beforeSuccess")); assertTrue(chainedListener.beforeMethodAfter.contains("beforeSuccess")); assertTrue(chainedListener.testMethodBefore.contains("testSkipped")); assertTrue(chainedListener.testMethodAfter.contains("testSkipped")); assertTrue(chainedListener.afterMethodBefore.contains("afterSuccess")); assertTrue(chainedListener.afterMethodAfter.contains("afterSuccess")); assertTrue(chainedListener.testStarted.contains("testSkipped")); assertTrue(chainedListener.testSkipped.contains("testSkipped")); }
@Test public void verifyFailAndPass() { ListenerChain lc = new ListenerChain(); TestListenerAdapter tla = new TestListenerAdapter(); TestNG testNG = new TestNG(); testNG.setTestClasses(new Class[]{ListenerChainTestCases.class}); testNG.addListener((ITestNGListener) lc); testNG.addListener((ITestNGListener) tla); testNG.setGroups("failAndPass"); testNG.run(); ChainedListener chainedListener = lc.getAttachedListener(ChainedListener.class).get(); assertEquals(tla.getPassedTests().size(), 3, "Incorrect passed test count"); assertEquals(tla.getFailedTests().size(), 0, "Incorrect failed test count"); assertEquals(tla.getSkippedTests().size(), 0, "Incorrect skipped test count"); assertEquals(tla.getFailedButWithinSuccessPercentageTests().size(), 1, "Incorrect curve-graded success count"); assertEquals(tla.getConfigurationFailures().size(), 0, "Incorrect configuration method failure count"); assertEquals(tla.getConfigurationSkips().size(), 0, "Incorrect configuration method skip count"); assertTrue(chainedListener.configSuccess.contains("beforeSuccess")); assertTrue(chainedListener.configSuccess.contains("afterSuccess")); assertTrue(chainedListener.beforeConfig.contains("beforeSuccess")); assertTrue(chainedListener.beforeConfig.contains("afterSuccess")); assertTrue(chainedListener.beforeMethodBefore.contains("beforeSuccess")); assertTrue(chainedListener.beforeMethodAfter.contains("beforeSuccess")); assertTrue(chainedListener.testMethodBefore.contains("failAndPass")); assertTrue(chainedListener.testMethodAfter.contains("failAndPass")); assertTrue(chainedListener.afterMethodBefore.contains("afterSuccess")); assertTrue(chainedListener.afterMethodAfter.contains("afterSuccess")); assertTrue(chainedListener.testStarted.contains("failAndPass")); assertTrue(chainedListener.testCurved.contains("failAndPass")); }
@Test public void verifyBeforeFailed() { ListenerChain lc = new ListenerChain(); TestListenerAdapter tla = new TestListenerAdapter(); TestNG testNG = new TestNG(); testNG.setTestClasses(new Class[]{ListenerChainTestCases.class}); testNG.addListener((ITestNGListener) lc); testNG.addListener((ITestNGListener) tla); testNG.setGroups("beforeFailed"); testNG.run(); ChainedListener chainedListener = lc.getAttachedListener(ChainedListener.class).get(); assertEquals(tla.getPassedTests().size(), 0, "Incorrect passed test count"); assertEquals(tla.getFailedTests().size(), 0, "Incorrect failed test count"); assertEquals(tla.getSkippedTests().size(), 1, "Incorrect skipped test count"); assertEquals(tla.getFailedButWithinSuccessPercentageTests().size(), 0, "Incorrect curve-graded success count"); assertEquals(tla.getConfigurationFailures().size(), 1, "Incorrect configuration method failure count"); assertEquals(tla.getConfigurationSkips().size(), 1, "Incorrect configuration method skip count"); assertTrue(chainedListener.configFailure.contains("beforeFailure")); assertTrue(chainedListener.configSkipped.contains("afterSuccess")); assertTrue(chainedListener.beforeConfig.contains("beforeFailure")); assertTrue(chainedListener.beforeMethodBefore.contains("beforeFailure")); assertTrue(chainedListener.beforeMethodAfter.contains("beforeFailure")); assertTrue(chainedListener.testMethodBefore.contains("skipBeforeFailed")); assertTrue(chainedListener.testMethodAfter.contains("skipBeforeFailed")); assertTrue(chainedListener.testStarted.contains("skipBeforeFailed")); assertTrue(chainedListener.testSkipped.contains("skipBeforeFailed")); }
@Test public void verifyBeforeSkipped() { ListenerChain lc = new ListenerChain(); TestListenerAdapter tla = new TestListenerAdapter(); TestNG testNG = new TestNG(); testNG.setTestClasses(new Class[]{ListenerChainTestCases.class}); testNG.addListener((ITestNGListener) lc); testNG.addListener((ITestNGListener) tla); testNG.setGroups("beforeSkipped"); testNG.run(); ChainedListener chainedListener = lc.getAttachedListener(ChainedListener.class).get(); assertEquals(tla.getPassedTests().size(), 0, "Incorrect passed test count"); assertEquals(tla.getFailedTests().size(), 0, "Incorrect failed test count"); assertEquals(tla.getSkippedTests().size(), 1, "Incorrect skipped test count"); assertEquals(tla.getFailedButWithinSuccessPercentageTests().size(), 0, "Incorrect curve-graded success count"); assertEquals(tla.getConfigurationFailures().size(), 0, "Incorrect configuration method failure count"); assertEquals(tla.getConfigurationSkips().size(), 2, "Incorrect configuration method skip count"); assertTrue(chainedListener.configSkipped.contains("beforeSkipped")); assertTrue(chainedListener.configSkipped.contains("afterSuccess")); assertTrue(chainedListener.beforeConfig.contains("beforeSkipped")); assertTrue(chainedListener.beforeMethodBefore.contains("beforeSkipped")); assertTrue(chainedListener.beforeMethodAfter.contains("beforeSkipped")); assertTrue(chainedListener.testMethodBefore.contains("skipBeforeSkipped")); assertTrue(chainedListener.testMethodAfter.contains("skipBeforeSkipped")); assertTrue(chainedListener.testStarted.contains("skipBeforeSkipped")); assertTrue(chainedListener.testSkipped.contains("skipBeforeSkipped")); }
@Test public void verifyAfterFailed() { ListenerChain lc = new ListenerChain(); TestListenerAdapter tla = new TestListenerAdapter(); TestNG testNG = new TestNG(); testNG.setTestClasses(new Class[]{ListenerChainTestCases.class}); testNG.addListener((ITestNGListener) lc); testNG.addListener((ITestNGListener) tla); testNG.setGroups("afterFailed"); testNG.run(); ChainedListener chainedListener = lc.getAttachedListener(ChainedListener.class).get(); assertEquals(tla.getPassedTests().size(), 1, "Incorrect passed test count"); assertEquals(tla.getFailedTests().size(), 0, "Incorrect failed test count"); assertEquals(tla.getSkippedTests().size(), 0, "Incorrect skipped test count"); assertEquals(tla.getFailedButWithinSuccessPercentageTests().size(), 0, "Incorrect curve-graded success count"); assertEquals(tla.getConfigurationFailures().size(), 1, "Incorrect configuration method failure count"); assertEquals(tla.getConfigurationSkips().size(), 0, "Incorrect configuration method skip count"); assertTrue(chainedListener.configSuccess.contains("beforeSuccess")); assertTrue(chainedListener.configFailure.contains("afterFailure")); assertTrue(chainedListener.beforeConfig.contains("beforeSuccess")); assertTrue(chainedListener.beforeConfig.contains("afterFailure")); assertTrue(chainedListener.beforeMethodBefore.contains("beforeSuccess")); assertTrue(chainedListener.beforeMethodAfter.contains("beforeSuccess")); assertTrue(chainedListener.testMethodBefore.contains("testAfterFailed")); assertTrue(chainedListener.testMethodAfter.contains("testAfterFailed")); assertTrue(chainedListener.afterMethodBefore.contains("afterFailure")); assertTrue(chainedListener.afterMethodAfter.contains("afterFailure")); assertTrue(chainedListener.testStarted.contains("testAfterFailed")); assertTrue(chainedListener.testSuccess.contains("testAfterFailed")); }
@Test public void verifyAfterSkipped() { ListenerChain lc = new ListenerChain(); TestListenerAdapter tla = new TestListenerAdapter(); TestNG testNG = new TestNG(); testNG.setTestClasses(new Class[]{ListenerChainTestCases.class}); testNG.addListener((ITestNGListener) lc); testNG.addListener((ITestNGListener) tla); testNG.setGroups("afterSkipped"); testNG.run(); ChainedListener chainedListener = lc.getAttachedListener(ChainedListener.class).get(); assertEquals(tla.getPassedTests().size(), 1, "Incorrect passed test count"); assertEquals(tla.getFailedTests().size(), 0, "Incorrect failed test count"); assertEquals(tla.getSkippedTests().size(), 0, "Incorrect skipped test count"); assertEquals(tla.getFailedButWithinSuccessPercentageTests().size(), 0, "Incorrect curve-graded success count"); assertEquals(tla.getConfigurationFailures().size(), 0, "Incorrect configuration method failure count"); assertEquals(tla.getConfigurationSkips().size(), 1, "Incorrect configuration method skip count"); assertTrue(chainedListener.configSuccess.contains("beforeSuccess")); assertTrue(chainedListener.configSkipped.contains("afterSkipped")); assertTrue(chainedListener.beforeConfig.contains("beforeSuccess")); assertTrue(chainedListener.beforeConfig.contains("afterSkipped")); assertTrue(chainedListener.beforeMethodBefore.contains("beforeSuccess")); assertTrue(chainedListener.beforeMethodAfter.contains("beforeSuccess")); assertTrue(chainedListener.testMethodBefore.contains("testAfterSkipped")); assertTrue(chainedListener.testMethodAfter.contains("testAfterSkipped")); assertTrue(chainedListener.afterMethodBefore.contains("afterSkipped")); assertTrue(chainedListener.afterMethodAfter.contains("afterSkipped")); assertTrue(chainedListener.testStarted.contains("testAfterSkipped")); assertTrue(chainedListener.testSuccess.contains("testAfterSkipped")); }
@SuppressWarnings("unchecked") static boolean shouldBeInvoked(Class testClass, Class<? extends ITestNGListener> listener){ if(!testClass.isAnnotationPresent(Listeners.class)) return false; if(asList(((Listeners) testClass.getAnnotation(Listeners.class)).value()).contains(listener)) return true; return shouldBeInvoked(testClass.getSuperclass(), listener); }
@SuppressWarnings("unchecked") static <T> T run(final String listenerClass, final Class<?>... testClasses) { final TestNG tng = create(testClasses); final InvokedMethodNameListener listener = new InvokedMethodNameListener(); final DataProviderTransformer dataProviderTransformer = new DataProviderTransformer(); tng.addListener((ITestNGListener) listener); tng.addListener(dataProviderTransformer); tng.setDefaultTestName("DataSupplier tests"); tng.run(); return (T) (listenerClass.equals(InvokedMethodNameListener.class.getName()) ? listener : dataProviderTransformer); }
protected boolean hasMockitoTestNGListener(Class<?> clazz) { Listeners listeners = clazz.getAnnotation(Listeners.class); if (listeners == null) { return false; } for (Class<? extends ITestNGListener> listenerClass : listeners.value()) { if (listenerClass() == listenerClass) { return true; } } return false; }
@Test public void verifyHappyPath() { ListenerChain lc = new ListenerChain(); TestListenerAdapter tla = new TestListenerAdapter(); TestNG testNG = new TestNG(); testNG.setTestClasses(new Class[]{ListenerChainTestCases.class, ListenerChainTestFactory.class}); testNG.addListener((ITestNGListener) lc); testNG.addListener((ITestNGListener) tla); testNG.setGroups("happyPath"); testNG.run(); ChainedListener chainedListener = lc.getAttachedListener(ChainedListener.class).get(); assertEquals(tla.getPassedTests().size(), 2, "Incorrect passed test count"); assertEquals(tla.getFailedTests().size(), 0, "Incorrect failed test count"); assertEquals(tla.getSkippedTests().size(), 0, "Incorrect skipped test count"); assertEquals(tla.getFailedButWithinSuccessPercentageTests().size(), 0, "Incorrect curve-graded success count"); assertEquals(tla.getConfigurationFailures().size(), 0, "Incorrect configuration method failure count"); assertEquals(tla.getConfigurationSkips().size(), 0, "Incorrect configuration method skip count"); assertTrue(chainedListener.configSuccess.contains("beforeSuccess")); assertTrue(chainedListener.configSuccess.contains("afterSuccess")); assertTrue(chainedListener.beforeConfig.contains("beforeSuccess")); assertTrue(chainedListener.beforeConfig.contains("afterSuccess")); assertTrue(chainedListener.beforeMethodBefore.contains("beforeSuccess")); assertTrue(chainedListener.beforeMethodAfter.contains("beforeSuccess")); assertTrue(chainedListener.testMethodBefore.contains("happyPath")); assertTrue(chainedListener.testMethodAfter.contains("happyPath")); assertTrue(chainedListener.afterMethodBefore.contains("afterSuccess")); assertTrue(chainedListener.afterMethodAfter.contains("afterSuccess")); assertTrue(chainedListener.testStarted.contains("happyPath")); assertTrue(chainedListener.testSuccess.contains("happyPath")); assertTrue(chainedListener.beforeClass.contains("ListenerChainTestCases")); assertTrue(chainedListener.afterClass.contains("ListenerChainTestCases")); assertTrue(chainedListener.testsBegun.contains("Command line test")); assertTrue(chainedListener.testsEnded.contains("Command line test")); assertTrue(chainedListener.suiteBegun.contains("Command line suite")); assertTrue(chainedListener.suiteEnded.contains("Command line suite")); Set<String> expectTests = new HashSet<>(Arrays.asList("method: testSkipped", "method: happyPath", "method: beforeSuccess", "method: beforeSkipped", "method: skipBeforeFailed", "method: skipBeforeSkipped", "method: testAfterSkipped", "method: productTest", "method: failAndPass", "method: afterSuccess", "method: afterFailure", "class: ListenerChainTestCases", "method: testAfterFailed", "method: beforeFailure", "method: afterSkipped", "method: testFailed", "method: testAttachedListeners")); Set<String> expectConfigs = new HashSet<>(Arrays.asList("method: afterSuccess", "method: afterFailure", "method: beforeSuccess", "method: beforeFailure", "method: beforeSkipped", "method: afterSkipped")); assertEquals(chainedListener.xformTest, expectTests); assertEquals(chainedListener.xformConfig, expectConfigs); assertTrue(chainedListener.xformProvider.contains("method: dataProvider")); assertTrue(chainedListener.xformFactory.contains("method: createInstances")); assertTrue(chainedListener.xformListeners.contains("class: ListenerChainTestCases")); assertTrue(chainedListener.interceptor.contains("Command line test")); }
@Test public void verifyConstructorFactory(){ ListenerChain lc = new ListenerChain(); TestListenerAdapter tla = new TestListenerAdapter(); TestNG testNG = new TestNG(); testNG.setTestClasses(new Class[]{ConstructorFactory.class}); testNG.addListener((ITestNGListener) lc); testNG.addListener((ITestNGListener) tla); testNG.run(); ChainedListener chainedListener = lc.getAttachedListener(ChainedListener.class).get(); assertEquals(tla.getPassedTests().size(), 3, "Incorrect passed test count"); assertEquals(tla.getFailedTests().size(), 0, "Incorrect failed test count"); assertEquals(tla.getSkippedTests().size(), 0, "Incorrect skipped test count"); assertEquals(tla.getFailedButWithinSuccessPercentageTests().size(), 0, "Incorrect curve-graded success count"); assertEquals(tla.getConfigurationFailures().size(), 0, "Incorrect configuration method failure count"); assertEquals(tla.getConfigurationSkips().size(), 0, "Incorrect configuration method skip count"); assertTrue(chainedListener.testMethodBefore.contains("fakeTest")); assertTrue(chainedListener.testMethodAfter.contains("fakeTest")); assertTrue(chainedListener.testStarted.contains("fakeTest")); assertTrue(chainedListener.testSuccess.contains("fakeTest")); assertTrue(chainedListener.beforeClass.contains("ConstructorFactory")); assertTrue(chainedListener.afterClass.contains("ConstructorFactory")); assertTrue(chainedListener.testsBegun.contains("Command line test")); assertTrue(chainedListener.testsEnded.contains("Command line test")); assertTrue(chainedListener.suiteBegun.contains("Command line suite")); assertTrue(chainedListener.suiteEnded.contains("Command line suite")); assertTrue(chainedListener.xformTest.contains("method: fakeTest")); assertTrue(chainedListener.xformProvider.contains("method: ints")); assertTrue(chainedListener.xformFactory.contains("ctor: (unknown)")); assertTrue(chainedListener.interceptor.contains("Command line test")); }
public static boolean inheritsITestListener(@NotNull PsiClass psiClass) { final Project project = psiClass.getProject(); final PsiClass aListenerClass = JavaPsiFacade.getInstance(project) .findClass(ITestNGListener.class.getName(), GlobalSearchScope.allScope(project)); return aListenerClass != null && psiClass.isInheritor(aListenerClass, true); }
/** * Get reference to an instance of the specified listener type. * * @param <T> listener type * @param result TestNG test result object * @param listenerType listener type * @return optional listener instance */ public static <T extends ITestNGListener> Optional<T> getAttachedListener(ITestResult result, Class<T> listenerType) { Objects.requireNonNull(result, "[result] must be non-null"); return getAttachedListener(result.getTestContext(), listenerType); }
/** * Get reference to an instance of the specified listener type. * * @param <T> listener type * @param context TestNG test context object * @param listenerType listener type * @return optional listener instance */ public static <T extends ITestNGListener> Optional<T> getAttachedListener(ITestContext context, Class<T> listenerType) { Objects.requireNonNull(context, "[context] must be non-null"); return getAttachedListener(context.getSuite(), listenerType); }