How to use StepFunction class of com.tngtech.jgiven package

Best JGiven code snippet using com.tngtech.jgiven.StepFunction

Source:ServeCoffeeTest.java Github

copy

Full Screen

...4import org.junit.Test;5import org.junit.runner.RunWith;6import com.tngtech.java.junit.dataprovider.DataProvider;7import com.tngtech.java.junit.dataprovider.DataProviderRunner;8import com.tngtech.jgiven.StepFunction;9import com.tngtech.jgiven.annotation.Description;10import com.tngtech.jgiven.examples.coffeemachine.steps.GivenCoffee;11import com.tngtech.jgiven.examples.coffeemachine.steps.ThenCoffee;12import com.tngtech.jgiven.examples.coffeemachine.steps.WhenCoffee;13import com.tngtech.jgiven.examples.tags.Order;14import com.tngtech.jgiven.examples.tags.TagsWithCustomStyle;15import com.tngtech.jgiven.junit.ScenarioTest;16import com.tngtech.jgiven.tags.FeatureCaseDiffs;17import com.tngtech.jgiven.tags.FeatureDataTables;18import com.tngtech.jgiven.tags.Issue;19/**20 * Original example due to Cucumber Wiki.21 */22@RunWith( DataProviderRunner.class )23@Description( "In order to refresh myself</br>" +24 "as a customer</br>" +25 "I want coffee to be served" )26public class ServeCoffeeTest extends ScenarioTest<GivenCoffee, WhenCoffee, ThenCoffee> {27 @Test28 @Order( "1" )29 public void an_empty_coffee_machine_cannot_serve_any_coffee() throws Exception {30 given().an_empty_coffee_machine();31 when().I_insert_$_one_euro_coins( 5 )32 .and().I_press_the_coffee_button();33 then().an_error_should_be_shown()34 .and().no_coffee_should_be_served();35 }36 @Test37 @Order( "2" )38 public void no_coffee_left_error_is_shown_when_there_is_no_coffee_left() {39 given().an_empty_coffee_machine();40 when().I_insert_$_one_euro_coins( 5 )41 .and().I_press_the_coffee_button();42 then().the_message_$_is_shown( "Error: No coffees left" );43 }44 @Test45 @Order( "3" )46 public void not_enough_money_message_is_shown_when_insufficient_money_was_given() throws Exception {47 given().a_coffee_machine()48 .and().there_are_$_coffees_left_in_the_machine( 2 );49 when().I_insert_$_one_euro_coins( 1 )50 .and().I_press_the_coffee_button();51 then().the_message_$_is_shown( "Error: Insufficient money" );52 }53 @Test54 @FeatureDataTables55 @DataProvider( {56 "0, 0, Error: No coffees left",57 "0, 1, Error: No coffees left",58 "1, 0, Error: Insufficient money",59 "0, 5, Error: No coffees left",60 "1, 5, Enjoy your coffee!",61 } )62 public void correct_messages_are_shown( int coffees_left, int number_of_coins, String message ) throws Exception {63 given().a_coffee_machine()64 .and().there_are_$_coffees_left_in_the_machine( coffees_left );65 when().I_insert_$_one_euro_coins( number_of_coins )66 .and().I_press_the_coffee_button();67 then().the_message_$_is_shown( message );68 }69 @Test70 @FeatureDataTables71 @Issue( "#15" )72 @DataProvider( { "1", "3", "10" } )73 public void serving_a_coffee_reduces_the_number_of_available_coffees_by_one( int initial_coffees ) {74 given().a_coffee_machine()75 .and().there_are_$_coffees_left_in_the_machine( initial_coffees );76 when().I_insert_$_one_euro_coins( 2 )77 .and().I_press_the_coffee_button();78 then().a_coffee_should_be_served()79 .and().there_are_$_coffees_left_in_the_machine( initial_coffees - 1 );80 }81 @Test82 public void a_turned_off_coffee_machine_cannot_serve_coffee() throws Exception {83 given().a_coffee_machine()84 .and().the_machine_is_turned_off();85 when().I_press_the_coffee_button();86 then().no_coffee_should_be_served();87 }88 @TagsWithCustomStyle89 @Test90 @DataProvider( {91 "true, 1, 1, false",92 "true, 1, 2, true",93 "true, 0, 2, false",94 "false, 1, 2, false",95 } )96 public void buy_a_coffee( boolean onOrOff, int coffees, int dollars, boolean shouldOrShouldNot ) {97 given().a_coffee_machine().and().there_are_$_coffees_left_in_the_machine( coffees ).and().the_machine_is_$onOrOff( onOrOff ).and()98 .the_coffee_costs_$_euros( 2 );99 when().I_insert_$_one_euro_coins( dollars ).and().I_press_the_coffee_button();100 then().I_$shouldOrShouldNot_be_served_a_coffee( shouldOrShouldNot );101 }102 @Test103 @FeatureCaseDiffs104 @DataProvider( { "true", "false" } )105 public void turned_off_machines_should_not_serve_coffee( boolean onOrOff ) {106 given().a_coffee_machine()107 .and().there_are_$_coffees_left_in_the_machine( 2 )108 .and().the_machine_is_$onOrOff( onOrOff );109 when().I_insert_$_one_euro_coins( 2 ).and().I_press_the_coffee_button();110 if( onOrOff ) {111 then().I_should_be_served_a_coffee();112 } else {113 then().I_should_not_be_served_a_coffee().and().no_error_is_shown();114 }115 }116 @Test117 @FailingOnPurpose118 public void a_failing_scenario_for_demonstration_purposes() {119 given().a_coffee_machine()120 .and().there_are_no_more_coffees_left();121 when().I_press_the_coffee_button();122 then().I_should_be_served_a_coffee()123 .and().steps_following_a_failed_step_should_be_skipped();124 }125 @Test126 @FailingOnPurpose127 @DataProvider( {128 "true",129 "false"130 } )131 public void a_scenario_with_a_failing_test_case_for_demonstration_purposes( boolean withCoffees ) {132 given().a_coffee_machine();133 if( withCoffees ) {134 given().and().there_are_$_coffees_left_in_the_machine( 2 );135 }136 when().I_insert_$_one_euro_coins( 2 ).and().I_press_the_coffee_button();137 then().I_should_be_served_a_coffee();138 }139 @Test140 public void intro_words_are_not_required() {141 given().a_coffee_machine()142 .the_coffee_costs_$_euros( 5 )143 .there_are_$_coffees_left_in_the_machine( 3 );144 when().I_press_the_coffee_button();145 then().an_error_should_be_shown()146 .no_coffee_should_be_served();147 }148 // tag::dataprovider[]149 @Test150 @DataProvider( {151 "1, 1",152 "0, 2",153 "1, 0"154 } )155 public void coffee_is_not_served( int coffees, int euros ) {156 given().a_coffee_machine()157 .and().the_coffee_costs_$_euros( 2 )158 .and().there_are_$_coffees_left_in_the_machine( coffees );159 when().I_insert_$_one_euro_coins( euros )160 .and().I_press_the_coffee_button();161 then().I_should_not_be_served_a_coffee();162 }163 // end::dataprovider[]164 @FailingOnPurpose165 @Test( timeout = 1000 )166 public void shouldFailWithUnexpectedRuntimeException() throws Exception {167 then().$( "should throw a runtime exception", //$NON-NLS-1$168 new StepFunction<ThenCoffee>() {169 @Override170 public void apply( final ThenCoffee stage )171 throws Exception {172 Thread.sleep( 2000 );173 }174 } );175 }176 // tag::casedescription[]177 @Test178 @DataProvider( {179 "On the first run, 1, quite ok",180 "And on the second run, 2, well-done"181 } )182 @CaseAs( "$1" )...

Full Screen

Full Screen

Source:StageBase.java Github

copy

Full Screen

1package com.tngtech.jgiven.base;2import com.tngtech.jgiven.StepFunction;3import com.tngtech.jgiven.annotation.Hidden;4/**5 * Useful base class for step definitions as it provides a {@link #self()} method6 * to create fluent interfaces.7 * <p>8 * Direct subclasses should provide introduction words (see {@link com.tngtech.jgiven.annotation.IntroWord}).9 * <p>10 * Typically one derives from one of the language-specific step definition classes, which11 * already provide a handful of useful introduction words.12 *13 * @param <SELF> the type of the subclass14 * @see com.tngtech.jgiven.Stage15 * @see com.tngtech.jgiven.lang.de.Stufe16 */17public class StageBase<SELF extends StageBase<?>> {18 @Hidden19 @SuppressWarnings( "unchecked" )20 public SELF self() {21 return (SELF) this;22 }23 /**24 * A step method for creating ad-hoc steps using lambdas.25 *26 * <h2>Example Usage</h2>27 * <pre>{@code 28 * given().$( "Two negative arguments", stage -> {29 * stage.given().argument( -5 )30 * .and().argument( -6 );31 * });32 * }</pre>33 *34 * @param description the description of the step35 * @param function the implementation of the step in form of a function where the parameter is the stage the step is executed in36 * @since 0.7.137 */38 public SELF $( String description, @Hidden StepFunction<? super SELF> function ) throws Exception {39 function.apply( self() );40 return self();41 }42 /**43 * A simple helper to provide a description44 *45 * <h2>Example Usage</h2>46 * <pre>{@code47 * given().$( "a description to improve the report, but is not related to any code that needs to be executed");48 * });49 * }</pre>50 *51 * @param description the description of the step52 * @since 0.15.4...

Full Screen

Full Screen

Source:StepFunction.java Github

copy

Full Screen

1package com.tngtech.jgiven;2/**3 * A functional interface for defining ad-hoc steps.4 *5 * @see Stage#$(String, StepFunction)6 * @param <STAGE> the stage in which this step is executed7 * @since 0.7.1 8 */9public interface StepFunction<STAGE> {10 public void apply( STAGE stage ) throws Exception;11}...

Full Screen

Full Screen

StepFunction

Using AI Code Generation

copy

Full Screen

1import com.tngtech.jgiven.Stage;2import com.tngtech.jgiven.annotation.ExpectedScenarioState;3import com.tngtech.jgiven.annotation.ScenarioState;4import com.tngtech.jgiven.annotation.ScenarioStage;5import com.tngtech.jgiven.annotation.Table;6import com.tngtech.jgiven.annotation.TableHeader;7import com.tngtech.jgiven.annotation.TableRow;8import com.tngtech.jgiven.junit.ScenarioTest;9import com.tngtech.jgiven.tags.FeatureBasic;10import com.tngtech.jgiven.tags.FeatureDataTables;11import com.tngtech.jgiven.tags.Issue;12import com.tngtech.jgiven.tags.IssueLink;13import com.tngtech.jgiven.tags.IssueLinks;14import com.tngtech.jgiven.tags.IssueType;15import com.tngtech.jgiven.tags.IssueTypes;16import com.tngtech.jgiven.tags.IssueUrl;17import com.tngtech.jgiven.tags.IssueUrls;18import com.tngtech.jgiven.tags.IssueValue;19import com.tngtech.jgiven.tags.IssueValues;20import com.tngtech.jgiven.tags.IssueVersions;21import com.tngtech.jgiven.tags.IssueVersion;22import com.tngtech.jgiven.tags.IssueVersionType;23import com.tngtech.jgiven.tags.IssueVersionTypes;24import com.tngtech.jgiven.ta

Full Screen

Full Screen

StepFunction

Using AI Code Generation

copy

Full Screen

1import com.tngtech.jgiven.annotation.*;2import com.tngtech.jgiven.junit.*;3import com.tngtech.jgiven.junit.ScenarioTest;4import com.tngtech.jgiven.report.model.*;5import com.tngtech.jgiven.report.model.Word;6import org.junit.*;7import org.junit.runner.*;8import org.junit.runners.*;9import java.util.*;10import com.tngtech.jgiven.base.*;11import com.tngtech.jgiven.annotation.*;12import com.tngtech.jgiven.annotati

Full Screen

Full Screen

StepFunction

Using AI Code Generation

copy

Full Screen

1public class StepFunction{2 public void test1(){3 StepFunction stepFunction = new StepFunction();4 stepFunction.given().a_step_function().when().the_function_is_evaluated().then().the_result_is();5 }6 public StepFunction given(){7 return this;8 }9 public StepFunction a_step_function(){10 return this;11 }12 public StepFunction when(){13 return this;14 }15 public StepFunction the_function_is_evaluated(){16 return this;17 }18 public StepFunction then(){19 return this;20 }21 public StepFunction the_result_is(){22 return this;23 }24}

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run JGiven automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used methods in StepFunction

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful