Best Serenity JUnit code snippet using net.thucydides.junit.listeners.JUnitStepListener
Source:WhenListeningForTestEvents.java
...49 public void initMocks() {50 MockitoAnnotations.initMocks(this);51 Mockito.<Class<?>>when(failureDescription.getTestClass()).thenReturn(ScenarioWithSomeFailingTests.class);52 }53 private JUnitStepListener listener;54 private JUnitStepListener failureTestListener;55 @Before56 public void setupListeners() throws Exception {57 setupDefaultListener();58 setupFailureListener();59 }60 private void setupDefaultListener() throws Exception {61 listener = JUnitStepListener.withOutputDirectory(outputDirectory)62 .and().withPageFactory(pages)63 .and().withTestClass(MyTestCase.class)64 .and().build();65 stepFactory = new StepFactory(pages);66 listener.testRunStarted(Description.createSuiteDescription(MyTestCase.class));67 listener.testStarted(Description.createTestDescription(MyTestCase.class,"app_should_work"));68 }69 private void setupFailureListener() throws Exception {70 failureTestListener = JUnitStepListener.withOutputDirectory(outputDirectory)71 .and().withPageFactory(pages)72 .and().withTestClass(ScenarioWithSomeFailingTests.class)73 .and().build();74 stepFactory = new StepFactory(pages);75 failureTestListener.testRunStarted(Description.createSuiteDescription(ScenarioWithSomeFailingTests.class));76 failureTestListener.testStarted(Description.createTestDescription(ScenarioWithSomeFailingTests.class,"app_should_work"));77 }78 @Test79 public void there_should_be_no_failing_steps_at_the_start_of_the_test() throws Exception {80 assertThat(listener.hasRecordedFailures(), is(false));81 }82 @Test83 public void a_junit_listener_should_record_test_results() throws Exception {84 Failure failure = new Failure(failureDescription, new AssertionError("Test failed."));...
Source:SerenityJUnitLifecycleAdapterExtension.java
...16import java.util.Optional;17import static java.util.stream.Collectors.toList;18import static net.thucydides.core.steps.StepEventBus.getEventBus;19import static net.thucydides.core.steps.TestSourceType.TEST_SOURCE_JUNIT;20// Junit4: net.thucydides.junit.listeners.JUnitStepListener21public class SerenityJUnitLifecycleAdapterExtension implements BeforeEachCallback, AfterAllCallback, TestWatcher, LifecycleMethodExecutionExceptionHandler {22 @Override23 public void beforeEach(final ExtensionContext extensionContext) {24 notifyTestStarted(extensionContext, extensionContext.getDisplayName().replace("()", ""));25 final Method testMethod = extensionContext.getRequiredTestMethod();26 if (testMethod.isAnnotationPresent(Pending.class) && !testMethod.isAnnotationPresent(Manual.class)) {27 throw new PendingException();28 }29 }30 public void afterAll(final ExtensionContext extensionContext) {31 StepEventBus.getEventBus().testSuiteFinished();32 }33 @Override34 // net.thucydides.junit.listeners.JUnitStepListener.notifyTestFinished => Junit4: Called when an atomic test has finished, whether the test succeeds or fails.35 // not using AfterTestExecutionCallback.afterTestExecution as it is called before org.junit.jupiter.api.extension.TestWatcher.testFailed or org.junit.jupiter.api.extension.TestWatcher.testAborted36 public void testSuccessful(final ExtensionContext extensionContext) {37 notifyTestFinished();38 }39 @Override40 // Junit4: net.serenitybdd.junit.runners.SerenityRunner.processTestMethodAnnotationsFor41 public void testDisabled(ExtensionContext extensionContext, Optional<String> reason) {42 beforeEach(extensionContext);43 getEventBus().testIgnored();44 notifyTestFinished();45 }46 @Override47 public void testAborted(final ExtensionContext extensionContext, final Throwable cause) {48 if (cause instanceof ManualTestAbortedException) {...
Source:JUnitStepListener.java
...12import java.util.List;13/**14 * Intercepts JUnit events and reports them to Thucydides.15 */16public class JUnitStepListener extends RunListener {17 private BaseStepListener baseStepListener;18 private StepListener[] extraListeners;19 private Class<?> testClass;20 private boolean testStarted;21 public static JUnitStepListenerBuilder withOutputDirectory(File outputDirectory) {22 return new JUnitStepListenerBuilder(outputDirectory); 23 }24 25 protected JUnitStepListener(Class<?> testClass, BaseStepListener baseStepListener, StepListener... listeners) {26 testStarted = false;27 this.baseStepListener = baseStepListener;28 this.extraListeners = listeners;29 this.testClass = testClass;30 registerThucydidesListeners();31 }32 public void registerThucydidesListeners() {33 StepEventBus.getEventBus().registerListener(baseStepListener);34 for(StepListener listener : extraListeners) {35 StepEventBus.getEventBus().registerListener(listener);36 }37 }38 public BaseStepListener getBaseStepListener() {39 return baseStepListener;...
Source:JUnitStepListenerBuilder.java
...5import net.thucydides.core.steps.BaseStepListener;6import net.thucydides.core.steps.Listeners;7import net.thucydides.core.steps.StepListener;8import net.thucydides.junit.guice.JUnitInjectors;9import net.thucydides.junit.runners.ParameterizedJUnitStepListener;10import java.io.File;11public class JUnitStepListenerBuilder {12 private final File outputDirectory;13 private final Pages pageFactory;14 private final int parameterSetNumber;15 private final DataTable parametersTable;16 private final Class<?> testClass;17 public JUnitStepListenerBuilder(File outputDirectory) {18 this(outputDirectory, null, -1, null);19 }20 public JUnitStepListenerBuilder(File outputDirectory,21 Pages pageFactory) {22 this(outputDirectory, pageFactory, -1, null);23 }24 public JUnitStepListenerBuilder(File outputDirectory,25 Pages pageFactory,26 int parameterSetNumber) {27 this(outputDirectory, pageFactory, parameterSetNumber, null);28 }29 public JUnitStepListenerBuilder(File outputDirectory,30 Pages pageFactory,31 int parameterSetNumber,32 DataTable parametersTable) {33 this(outputDirectory, pageFactory, parameterSetNumber, parametersTable, null);34 }35 public JUnitStepListenerBuilder(File outputDirectory,36 Pages pageFactory,37 int parameterSetNumber,38 DataTable parametersTable,39 Class<?> testClass) {40 this.outputDirectory = outputDirectory;41 this.pageFactory = pageFactory;42 this.parameterSetNumber = parameterSetNumber;43 this.parametersTable = parametersTable;44 this.testClass = testClass;45 }46 public JUnitStepListenerBuilder and() {47 return this;48 }49 public JUnitStepListenerBuilder withPageFactory(Pages pageFactory) {50 return new JUnitStepListenerBuilder(outputDirectory, pageFactory);51 }52 public JUnitStepListenerBuilder withParameterSetNumber(int parameterSetNumber) {53 return new JUnitStepListenerBuilder(outputDirectory, pageFactory, parameterSetNumber);54 }55 public JUnitStepListenerBuilder withParametersTable(DataTable parametersTable) {56 return new JUnitStepListenerBuilder(outputDirectory, pageFactory, parameterSetNumber, parametersTable);57 }58 public JUnitStepListenerBuilder withTestClass(Class<?> testClass) {59 return new JUnitStepListenerBuilder(outputDirectory, pageFactory, parameterSetNumber, parametersTable, testClass);60 }61 public JUnitStepListener build() {62 if (parameterSetNumber >= 0) {63 return newParameterizedJUnitStepListener();64 } else {65 return newStandardJunitStepListener();66 }67 }68 private BaseStepListener buildBaseStepListener() {69 if (pageFactory != null) {70 return Listeners.getBaseStepListener()71 .withPages(pageFactory)72 .and().withOutputDirectory(outputDirectory);73 } else {74 return Listeners.getBaseStepListener()75 .withOutputDirectory(outputDirectory);76 }77 }78 private JUnitStepListener newParameterizedJUnitStepListener() {79 return new ParameterizedJUnitStepListener(parameterSetNumber,80 parametersTable,81 testClass,82 buildBaseStepListener(),83 Listeners.getLoggingListener(),84 newTestCountListener());85// Listeners.getStatisticsListener());86 }87 private StepListener newTestCountListener() {88 return JUnitInjectors.getInjector().getInstance(Key.get(StepListener.class, TestCounter.class));89 }90 private JUnitStepListener newStandardJunitStepListener() {91 return new JUnitStepListener(testClass,92 buildBaseStepListener(),93 Listeners.getLoggingListener(),94 newTestCountListener());95// Listeners.getStatisticsListener());96 }97}...
Source:ParameterizedJUnitStepListener.java
...3import net.thucydides.core.model.DataTable;4import net.thucydides.core.steps.BaseStepListener;5import net.thucydides.core.steps.StepEventBus;6import net.thucydides.core.steps.StepListener;7import net.thucydides.junit.listeners.JUnitStepListener;8import org.junit.runner.Description;9import org.junit.runner.notification.Failure;10public class ParameterizedJUnitStepListener extends JUnitStepListener {11 final int parameterSetNumber;12 private final DataTable parametersTable;13 public ParameterizedJUnitStepListener(final int parameterSetNumber, final DataTable parametersTable,14 final Class<?> testClass, BaseStepListener baseStepListener,15 StepListener... listeners) {16 super(testClass, baseStepListener, listeners);17 this.parameterSetNumber = parameterSetNumber;18 this.parametersTable = parametersTable;19 }20 @Override21 public void testRunStarted(Description description) throws Exception {22 super.testRunStarted(description);23 }24 @Override25 public void testStarted(final Description description) {26 if (testingThisDataSet(description)) {27 super.testStarted(description);...
Source:TestClassRunnerForParameters.java
...20 this.parametersTable = parametersTable;21 parameterSetNumber = i;22 }23 @Override24 protected JUnitStepListener initListenersUsing(final Pages pageFactory) {25 setStepListener(JUnitStepListener.withOutputDirectory(getConfiguration().getOutputDirectory())26 .and().withPageFactory(pageFactory)27 .and().withParameterSetNumber(parameterSetNumber)28 .and().withParametersTable(parametersTable)29 .and().withTestClass(getTestClass().getJavaClass())30 .and().build());31 return getStepListener();32 }33 @Override34 protected JUnitStepListener initListeners() {35 setStepListener(JUnitStepListener.withOutputDirectory(getConfiguration().getOutputDirectory())36 .and().withParameterSetNumber(parameterSetNumber)37 .and().withParametersTable(parametersTable)38 .and().withTestClass(getTestClass().getJavaClass())39 .and().build());40 return getStepListener();41 }42 @Override43 protected Object initializeTest() throws Exception {44 return getTestClass().getOnlyConstructor().newInstance(computeParams());45 }46 private Object[] computeParams() throws Exception {47 try {48 DataTableRow row = parametersTable.getRows().get(parameterSetNumber);49 return row.getValues().toArray();...
Source:TestClassRunnerForInstanciatedTestCase.java
...3import net.thucydides.core.model.DataTable;4import net.thucydides.core.pages.Pages;5import net.thucydides.core.webdriver.Configuration;6import net.thucydides.core.webdriver.WebDriverFactory;7import net.thucydides.junit.listeners.JUnitStepListener;8import org.junit.runner.notification.RunNotifier;9import org.junit.runners.model.FrameworkMethod;10import org.junit.runners.model.InitializationError;11import org.junit.runners.model.Statement;12class TestClassRunnerForInstanciatedTestCase extends ThucydidesRunner {13 private final int parameterSetNumber;14 private final Object instanciatedTest;15 private final DataTable parametersTable;16 TestClassRunnerForInstanciatedTestCase(final Object instanciatedTest,17 Configuration configuration,18 WebDriverFactory webDriverFactory,19 final BatchManager batchManager,20 final DataTable parametersTable,21 final int parameterSetNumber) throws InitializationError {22 super(instanciatedTest.getClass(), webDriverFactory, configuration, batchManager);23 this.instanciatedTest = instanciatedTest;24 this.parameterSetNumber = parameterSetNumber;25 this.parametersTable = parametersTable;26 }27 @Override28 protected JUnitStepListener initListenersUsing(final Pages pageFactory) {29 setStepListener(JUnitStepListener.withOutputDirectory(getConfiguration().getOutputDirectory())30 .and().withPageFactory(pageFactory)31 .and().withParameterSetNumber(parameterSetNumber)32 .and().withParametersTable(parametersTable)33 .and().withTestClass(getTestClass().getJavaClass())34 .and().build());35 return getStepListener();36 }37 @Override38 public Object createTest() throws Exception {39 return instanciatedTest;40 }41 @Override42 protected String getName() {43 return QualifierFinder.forTestCase(instanciatedTest).getQualifier();...
JUnitStepListener
Using AI Code Generation
1package net.thucydides.junit.runners;2import net.thucydides.junit.listeners.JUnitStepListener;3import net.thucydides.junit.listeners.JUnitStepListenerFactory;4import org.junit.runner.Description;5import org.junit.runner.Runner;6import org.junit.runner.notification.RunNotifier;7import org.junit.runners.BlockJUnit4ClassRunner;8import org.junit.runners.model.InitializationError;9public class ThucydidesRunner extends BlockJUnit4ClassRunner {10 private final JUnitStepListener stepListener;11 public ThucydidesRunner(Class<?> klass) throws InitializationError {12 super(klass);13 stepListener = JUnitStepListenerFactory.getListenerFor(getTestClass().getJavaClass());14 }15 public void run(RunNotifier notifier) {16 notifier.addListener(stepListener);17 super.run(notifier);18 notifier.removeListener(stepListener);19 }20 protected void runChild(Runner runner, RunNotifier notifier) {21 Description description = describeChild(runner);22 notifier.fireTestStarted(description);23 stepListener.testStarted(description);24 super.runChild(runner, notifier);25 stepListener.testFinished(description);26 notifier.fireTestFinished(description);27 }28}29package net.thucydides.junit.runners;30import net.thucydides.junit.runners.ThucydidesRunner;31import org.junit.runner.RunWith;32@RunWith(ThucydidesRunner.class)33public class YourTestClass {34}35package net.thucydides.junit.runners;36import net.thucydides.junit.runners.ThucydidesRunner;37import org.junit.runner.RunWith;38@RunWith(ThucydidesRunner.class)39public class YourTestClass {40}41package net.thucydides.junit.runners;42import net.thucydides.junit.runners.ThucydidesRunner;43import org.junit.runner.RunWith;44@RunWith(ThucydidesRunner.class)45public class YourTestClass {46}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!