Best JGiven code snippet using com.tngtech.jgiven.impl.util.ThrowableUtil
Source:StepInterceptorImpl.java
...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 }...
Source:ThrowableUtil.java
1package com.tngtech.jgiven.impl.util;2public class ThrowableUtil {3 public static boolean isAssumptionException(Throwable t) {4 return t.getClass().getName().equals( "org.junit.AssumptionViolatedException" )5 || t.getClass().getName().equals( "org.testng.SkipException");6 }7}...
ThrowableUtil
Using AI Code Generation
1import com.tngtech.jgiven.impl.util.ThrowableUtil;2public class TestJgiven {3 public static void main(String[] args) {4 try {5 throw new Exception("This is a test exception");6 } catch (Exception e) {7 System.out.println(ThrowableUtil.getStackTrace(e));8 }9 }10}11 at TestJgiven.main(TestJgiven.java:9)
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!!