Best JGiven code snippet using com.tngtech.jgiven.impl.StageLifecycleManager.LifecycyleMethodManager
Source:StageLifecycleManager.java
...19class StageLifecycleManager {20 private static final Logger log = LoggerFactory.getLogger(StageLifecycleManager.class);21 private final Object instance;22 private final StepInterceptorImpl methodInterceptor;23 private final LifecycyleMethodManager<AfterStage> afterStageRegister;24 private final LifecycyleMethodManager<BeforeStage> beforeStageRegister;25 private final LifecycyleMethodManager<BeforeScenario> beforeScenarioRegister;26 private final LifecycyleMethodManager<AfterScenario> afterScenarioRegister;27 StageLifecycleManager(Object instance, StepInterceptorImpl methodInterceptor) {28 this.methodInterceptor = methodInterceptor;29 this.instance = instance;30 afterStageRegister = new LifecycyleMethodManager<>(AfterStage.class, AfterStage::repeatable);31 beforeStageRegister = new LifecycyleMethodManager<>(BeforeStage.class, BeforeStage::repeatable);32 beforeScenarioRegister = new LifecycyleMethodManager<>(BeforeScenario.class, (it) -> false);33 afterScenarioRegister = new LifecycyleMethodManager<>(AfterScenario.class, (it) -> false);34 }35 boolean allAfterStageMethodsHaveBeenExecuted() {36 return afterStageRegister.allMethodsHaveBeenExecuted();37 }38 void executeAfterStageMethods(boolean fakeExecution) throws Throwable {39 executeLifecycleMethods(afterStageRegister, fakeExecution);40 }41 void executeBeforeStageMethods(boolean fakeExecution) throws Throwable {42 executeLifecycleMethods(beforeStageRegister, fakeExecution);43 }44 void executeAfterScenarioMethods(boolean fakeExecution) throws Throwable {45 executeLifecycleMethods(afterScenarioRegister, fakeExecution);46 }47 void executeBeforeScenarioMethods(boolean fakeExecution) throws Throwable {48 executeLifecycleMethods(beforeScenarioRegister, fakeExecution);49 }50 private void executeLifecycleMethods(LifecycyleMethodManager<?> register, boolean fakeExecution) throws Throwable {51 if (fakeExecution) {52 register.fakeExecution();53 } else {54 register.executeMethods();55 }56 }57 private enum StepExecutionState {58 EXECUTED(true),59 REPEATABLE(false),60 NOT_EXECUTED(false);61 private final boolean hasBeenExecuted;62 StepExecutionState(boolean hasBeenExecuted) {63 this.hasBeenExecuted = hasBeenExecuted;64 }65 public boolean toBoolean() {66 return this.hasBeenExecuted;67 }68 }69 private class LifecycyleMethodManager<T extends Annotation> {70 private final Class<T> targetAnnotation;71 private final Map<Method, StepExecutionState> register = new HashMap<>();72 private final Predicate<T> predicateFromT;73 private LifecycyleMethodManager(Class<T> targetAnnotation, Predicate<T> predicateFromAnnotation) {74 this.targetAnnotation = targetAnnotation;75 this.predicateFromT = predicateFromAnnotation;76 fillStageRegister(instance);77 }78 @SuppressWarnings({"unchecked"})79 private void fillStageRegister(Object instance) {80 ReflectionUtil.forEachMethod(instance, instance.getClass(), targetAnnotation,81 (object, method) ->82 Arrays.stream(method.getDeclaredAnnotations())83 .filter(annotation -> targetAnnotation.isAssignableFrom(annotation.getClass()))84 .map(annotation -> (T) annotation)85 .findFirst()86 .map(it -> predicateFromT.test(it) ? StepExecutionState.REPEATABLE :87 StepExecutionState.NOT_EXECUTED)...
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!!