Best JGiven code snippet using com.tngtech.jgiven.impl.ScenarioExecutor.finished
Source:ScenarioExecutor.java
...232 executeBeforeScenarioMethods(stage.instance);233 }234 } catch (Throwable e) {235 failed(e);236 finished();237 throw e;238 }239 methodInterceptor.enableMethodInterception(true);240 }241 private void invokeRuleMethod(Object rule, String methodName) throws Throwable {242 if (!executeLifeCycleMethods) {243 return;244 }245 Optional<Method> optionalMethod = ReflectionUtil.findMethodTransitively(rule.getClass(), methodName);246 if (!optionalMethod.isPresent()) {247 log.debug("Class {} has no {} method, but was used as ScenarioRule!", rule.getClass(), methodName);248 return;249 }250 try {251 ReflectionUtil.invokeMethod(rule, optionalMethod.get(), " of rule class " + rule.getClass().getName());252 } catch (JGivenUserException e) {253 throw e.getCause();254 }255 }256 private void executeBeforeScenarioMethods(Object stage) throws Throwable {257 getStageState(stage).executeBeforeScenarioMethods(!executeLifeCycleMethods);258 }259 private void executeBeforeStageMethods(Object stage) throws Throwable {260 getStageState(stage).executeBeforeStageMethods(!executeLifeCycleMethods);261 }262 private void executeAfterStageMethods(Object stage) throws Throwable {263 getStageState(stage).executeAfterStageMethods(!executeLifeCycleMethods);264 }265 private void executeAfterScenarioMethods(Object stage) throws Throwable {266 getStageState(stage).executeAfterScenarioMethods(!executeLifeCycleMethods);267 }268 public void readScenarioState(Object object) {269 injector.readValues(object);270 }271 /**272 * Used for DI frameworks to inject values into stages.273 */274 public void wireSteps(CanWire canWire) {275 for (StageState steps : stages.values()) {276 canWire.wire(steps.instance);277 }278 }279 /**280 * Has to be called when the scenario is finished in order to execute after methods.281 */282 public void finished() throws Throwable {283 if (state == FINISHED) {284 return;285 }286 State previousState = state;287 state = FINISHED;288 methodInterceptor.enableMethodInterception(false);289 try {290 if (previousState == STARTED) {291 callFinishLifeCycleMethods();292 }293 } finally {294 listener.scenarioFinished();295 }296 }...
Source:StepInterceptorImpl.java
1package com.tngtech.jgiven.impl.intercept;2import static com.tngtech.jgiven.report.model.InvocationMode.NORMAL;3import static com.tngtech.jgiven.report.model.InvocationMode.PENDING;4import static com.tngtech.jgiven.report.model.InvocationMode.SKIPPED;5import java.lang.reflect.Method;6import java.util.Arrays;7import java.util.List;8import java.util.Stack;9import com.tngtech.jgiven.impl.util.ThrowableUtil;10import org.slf4j.Logger;11import org.slf4j.LoggerFactory;12import com.tngtech.jgiven.annotation.DoNotIntercept;13import com.tngtech.jgiven.annotation.Hidden;14import com.tngtech.jgiven.annotation.NestedSteps;15import com.tngtech.jgiven.annotation.Pending;16import com.tngtech.jgiven.impl.ScenarioExecutor;17import com.tngtech.jgiven.impl.util.ParameterNameUtil;18import com.tngtech.jgiven.report.model.InvocationMode;19import com.tngtech.jgiven.report.model.NamedArgument;20public class StepInterceptorImpl implements StepInterceptor {21 private static final Logger log = LoggerFactory.getLogger( StepInterceptorImpl.class );22 private static final int INITIAL_MAX_STEP_DEPTH = 1;23 private ScenarioExecutor scenarioExecutor;24 private StageTransitionHandler stageTransitionHandler;25 private ScenarioListener listener;26 /**27 * Contains the stack of call receivers. This is used to update28 * the state of a parent stage after a call to a child stage has returned29 */30 protected final Stack<Object> stageStack = new Stack<Object>();31 private int maxStepDepth = INITIAL_MAX_STEP_DEPTH;32 private InvocationMode defaultInvocationMode = InvocationMode.NORMAL;33 /**34 * Whether methods should be intercepted or not35 */36 private boolean interceptingEnabled;37 /**38 * Whether step methods are actually executed or just skipped39 */40 private boolean methodExecutionEnabled = true;41 /**42 * Whether all exceptions should be suppressed and not be rethrown43 */44 private boolean suppressExceptions = true;45 public StepInterceptorImpl(ScenarioExecutor scenarioExecutor, ScenarioListener listener, StageTransitionHandler stageTransitionHandler) {46 this.scenarioExecutor = scenarioExecutor;47 this.listener = listener;48 this.stageTransitionHandler = stageTransitionHandler;49 }50 public final Object intercept( final Object receiver, Method method, final Object[] parameters, Invoker invoker ) throws Throwable {51 if( !shouldInterceptMethod( method ) ) {52 return invoker.proceed();53 }54 int currentStackDepth = stageStack.size();55 Object parentStage = null;56 if( !stageStack.isEmpty() ) {57 parentStage = stageStack.peek();58 }59 stageStack.push( receiver );60 try {61 stageTransitionHandler.enterStage( parentStage, receiver );62 return doIntercept( receiver, method, parameters, invoker, currentStackDepth );63 } finally {64 stageStack.pop();65 stageTransitionHandler.leaveStage( parentStage, receiver );66 }67 }68 private Object doIntercept(Object receiver, Method method, Object[] parameters, Invoker invoker, int currentStackDepth )69 throws Throwable {70 long started = System.nanoTime();71 InvocationMode mode = getInvocationMode( receiver, method );72 boolean hasNestedSteps = method.isAnnotationPresent( NestedSteps.class );73 boolean handleMethod = shouldHandleMethod( method );74 if( handleMethod ) {75 handleMethod( receiver, method, parameters, mode, hasNestedSteps );76 }77 if( mode == SKIPPED || mode == PENDING ) {78 return returnReceiverOrNull( receiver, method );79 }80 if( hasNestedSteps ) {81 maxStepDepth++;82 }83 try {84 return invoker.proceed();85 } catch( Exception e ) {86 return handleThrowable( receiver, method, e, System.nanoTime() - started, handleMethod );87 } catch( AssertionError e ) {88 return handleThrowable( receiver, method, e, System.nanoTime() - started, handleMethod );89 } finally {90 if( hasNestedSteps ) {91 maxStepDepth--;92 }93 if( handleMethod ) {94 handleMethodFinished( System.nanoTime() - started, hasNestedSteps );95 }96 }97 }98 private boolean shouldHandleMethod( Method method ) {99 if( method.isSynthetic() && !method.isBridge() ) {100 return false;101 }102 if( method.isAnnotationPresent( Hidden.class ) ) {103 return false;104 }105 if( stageStack.size() > maxStepDepth ) {106 return false;107 }108 return true;109 }110 private boolean shouldInterceptMethod( Method method ) {111 return interceptingEnabled112 && method.getDeclaringClass() != Object.class113 && !method.isAnnotationPresent(DoNotIntercept.class);114 }115 protected Object handleThrowable( Object receiver, Method method, Throwable t, long durationInNanos, boolean handleMethod )116 throws Throwable {117 if( handleMethod ) {118 handleThrowable( t );119 return returnReceiverOrNull( receiver, method );120 }121 throw t;122 }123 protected Object returnReceiverOrNull( Object receiver, Method method ) {124 // we assume here that the implementation follows the fluent interface125 // convention and returns the receiver object. If not, we fall back to null126 // and hope for the best.127 if( !method.getReturnType().isAssignableFrom( receiver.getClass() ) ) {128 if( method.getReturnType() != Void.class ) {129 log.warn( "The step method " + method.getName()130 + " of class " + method.getDeclaringClass().getSimpleName()131 + " does not follow the fluent interface convention of returning "132 + "the receiver object. Please change the return type to the SELF type parameter." );133 }134 return null;135 }136 return receiver;137 }138 protected InvocationMode getInvocationMode( Object receiver, Method method ) {139 if( !methodExecutionEnabled ) {140 return SKIPPED;141 }142 if( method.isAnnotationPresent( Pending.class )143 || method.getDeclaringClass().isAnnotationPresent( Pending.class )144 || receiver.getClass().isAnnotationPresent( Pending.class ) ) {145 return PENDING;146 }147 return defaultInvocationMode;148 }149 public void enableMethodInterception(boolean b ) {150 interceptingEnabled = b;151 }152 public void disableMethodExecution() {153 methodExecutionEnabled = false;154 }155 public boolean enableMethodExecution( boolean b ) {156 boolean previousMethodExecution = methodExecutionEnabled;157 methodExecutionEnabled = b;158 return previousMethodExecution;159 }160 public void setSuppressExceptions(boolean b) {161 suppressExceptions = b;162 }163 public void setDefaultInvocationMode(InvocationMode defaultInvocationMode) {164 this.defaultInvocationMode = defaultInvocationMode;165 }166 private void handleMethod(Object stageInstance, Method paramMethod, Object[] arguments, InvocationMode mode,167 boolean hasNestedSteps ) throws Throwable {168 List<NamedArgument> namedArguments = ParameterNameUtil.mapArgumentsWithParameterNames( paramMethod,169 Arrays.asList( arguments ) );170 listener.stepMethodInvoked( paramMethod, namedArguments, mode, hasNestedSteps );171 }172 private void handleThrowable( Throwable t ) throws Throwable {173 if( ThrowableUtil.isAssumptionException(t) ) {174 throw t;175 }176 listener.stepMethodFailed( t );177 scenarioExecutor.failed( t );178 if (!suppressExceptions) {179 throw t;180 }181 }182 private void handleMethodFinished( long durationInNanos, boolean hasNestedSteps ) {183 listener.stepMethodFinished( durationInNanos, hasNestedSteps );184 }185 public void setScenarioListener(ScenarioListener scenarioListener) {186 this.listener = scenarioListener;187 }188}...
Source:ScenarioBase.java
...42 * Finishes the scenario.43 *44 * @throws Throwable in case some exception has been thrown during the execution of the scenario45 */46 public void finished() throws Throwable {47 executor.finished();48 }49 public ScenarioExecutor getExecutor() {50 return executor;51 }52 public void setExecutor( ScenarioExecutor executor ) {53 assertNotInitialized();54 this.executor = executor;55 }56 public void wireSteps( CanWire canWire ) {57 executor.wireSteps( canWire );58 }59 public ScenarioBase startScenario( Class<?> testClass, Method method, List<NamedArgument> arguments ) {60 performInitialization();61 executor.startScenario( testClass, method, arguments );...
finished
Using AI Code Generation
1import com.tngtech.jgiven.annotation.*;2import com.tngtech.jgiven.impl.ScenarioExecutor;3import com.tngtech.jgiven.junit.ScenarioTest;4public class JGivenTest extends ScenarioTest<GivenStage, WhenStage, ThenStage> {5 private ScenarioExecutor scenarioExecutor;6 public void test() {7 given().some_state();8 when().some_action();9 scenarioExecutor.finished();10 then().some_outcome();11 }12}13import com.tngtech.jgiven.annotation.*;14import com.tngtech.jgiven.impl.ScenarioExecutor;15import com.tngtech.jgiven.junit.ScenarioTest;16public class JGivenTest extends ScenarioTest<GivenStage, WhenStage, ThenStage> {17 private ScenarioExecutor scenarioExecutor;18 public void test() {19 given().some_state();20 when().some_action();21 scenarioExecutor.finished();22 then().some_outcome();23 }24}25import com.tngtech.jgiven.annotation.*;26import com.tngtech.jgiven.impl.ScenarioExecutor;27import com.tngtech.jgiven.junit.ScenarioTest;28public class JGivenTest extends ScenarioTest<GivenStage, WhenStage, ThenStage> {29 private ScenarioExecutor scenarioExecutor;30 public void test() {31 given().some_state();32 when().some_action();33 scenarioExecutor.finished();34 then().some_outcome();35 }36}37import com.tngtech.jgiven.annotation.*;38import com.tngtech.jgiven.impl.ScenarioExecutor;39import com.tngtech.jgiven.junit.ScenarioTest;40public class JGivenTest extends ScenarioTest<GivenStage, WhenStage, ThenStage> {41 private ScenarioExecutor scenarioExecutor;42 public void test() {43 given().some_state();44 when().some_action();45 scenarioExecutor.finished();46 then().some_outcome();47 }48}
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!!