Best JGiven code snippet using com.tngtech.jgiven.BeforeAfterTestStage.TestRule
Source:ScenarioRuleTest.java
...8 public void testBeforeAndAfterIsCalled() throws Throwable {9 getScenario().setModel( new ReportModel() );10 getScenario().startScenario( "Some Scenario" );11 BeforeAfterTestStage<?> steps = given().something();12 TestRule rule = steps.rule;13 assertThat( rule.beforeCalled ).isEqualTo( 1 );14 assertThat( steps.beforeCalled ).isEqualTo( 1 );15 when();16 then();17 assertThat( rule.afterCalled ).isEqualTo( 0 );18 assertThat( steps.afterCalled ).isEqualTo( 0 );19 getScenario().finished();20 assertThat( rule.beforeCalled ).isEqualTo( 1 );21 assertThat( rule.afterCalled ).isEqualTo( 1 );22 assertThat( steps.beforeCalled ).isEqualTo( 1 );23 assertThat( steps.afterCalled ).isEqualTo( 1 );24 }25 @Test26 public void methods_annotated_with_AfterStage_are_called() {27 getScenario().startScenario( "methods_annotated_with_AfterStage_are_called" );28 given().something();29 when().something_happens();30 assertThat( getScenario().getWhenStage().afterStageCalled ).as( "afterStage has not been called" ).isEqualTo( 0 );31 assertThat( getScenario().getGivenStage().afterStageCalled ).as( "afterStage has been called" ).isEqualTo( 1 );32 }33 @Test34 public void afterStage_methods_are_only_invoked_once() {35 getScenario().startScenario( "methods_annotated_with_AfterStage_are_called_only_once" );36 given().something();37 when().something_happens();38 given().something();39 when().something_happens();40 assertThat( getScenario().getGivenStage().afterStageCalled ).as( "afterStage has been called" ).isEqualTo( 1 );41 }42 @Test43 public void whenExceptionThrownInStepThenAfterMethodsAreExecuted() {44 getScenario().startScenario( "some description" );45 BeforeAfterTestStage<?> steps = given();46 try {47 when().an_exception_is_thrown();48 } catch( Exception e ) {49 assertThat( steps.rule.afterCalled ).isEqualTo( 1 );50 assertThat( steps.afterCalled ).isEqualTo( 1 );51 }52 }53 @Test54 public void whenExceptionThrownInBeforeOfRuleThenAfterMethodIsStillCalled() {55 ExceptionStep steps = getScenario().addStage( ExceptionStep.class );56 try {57 getScenario().startScenario( "some description" );58 steps.given().something();59 } catch( Exception e ) {60 TestRule rule = steps.exceptionRule;61 assertThat( rule.beforeCalled ).isEqualTo( 1 );62 assertThat( rule.afterCalled ).isEqualTo( 1 );63 assertThat( steps.beforeCalled ).isEqualTo( 0 );64 assertThat( steps.afterCalled ).isEqualTo( 0 );65 }66 }67 static class ExceptionStep extends BeforeAfterTestStage<ExceptionStep> {68 @ScenarioRule69 BeforeExceptionRule exceptionRule = new BeforeExceptionRule();70 }71 static class BeforeExceptionRule extends TestRule {72 @Override73 void before() {74 super.before();75 throw new RuntimeException();76 }77 }78 public static class TestRule {79 public int beforeCalled;80 public int afterCalled;81 void before() {82 beforeCalled++;83 }84 void after() {85 afterCalled++;86 }87 }88}...
Source:BeforeAfterTestStage.java
1package com.tngtech.jgiven;2import com.tngtech.jgiven.ScenarioRuleTest.TestRule;3import com.tngtech.jgiven.annotation.AfterScenario;4import com.tngtech.jgiven.annotation.AfterStage;5import com.tngtech.jgiven.annotation.BeforeScenario;6import com.tngtech.jgiven.annotation.ScenarioRule;7public class BeforeAfterTestStage<SELF extends BeforeAfterTestStage<?>> extends Stage<SELF> {8 @ScenarioRule9 public TestRule rule = new TestRule();10 public int beforeCalled;11 public int afterCalled;12 public int afterStageCalled;13 public SELF something() {14 return self();15 }16 @AfterStage17 void someAfterStageMethod() {18 afterStageCalled++;19 }20 @BeforeScenario21 void someBeforeMethod() {22 beforeCalled++;23 }...
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!!