Best JGiven code snippet using com.tngtech.jgiven.examples.coffeemachine.steps.WhenCoffee.I_press_the_coffee_button
Source:ServeCoffeeTest.java
...28 @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 } );...
Source:JUnitParamsServeCoffeeTest.java
...29 and().there_are_$_coffees_left_in_the_machine( coffees ).30 and().the_machine_is_$onOrOff(onOrOff).31 and().the_coffee_costs_$_euros( 2 );32 when().I_insert_$_one_euro_coins( dollars ).33 and().I_press_the_coffee_button();34 then().I_$shouldOrShouldNot_be_served_a_coffee( shouldOrShouldNot );35 }36}...
Source:WhenCoffee.java
...11 public WhenCoffee I_insert_$_one_euro_coins( int euros ) {12 coffeeMachine.insertOneEuroCoin( euros );13 return self();14 }15 public WhenCoffee I_press_the_coffee_button() {16 coffeeServed = coffeeMachine.pressButton();17 return self();18 }19 public WhenCoffee I_make_coffee_for_the_$_time(int nr) {20 coffeeMachine.coffeCount = nr;21 return self();22 }23}...
I_press_the_coffee_button
Using AI Code Generation
1package com.tngtech.jgiven.examples.coffeemachine.steps;2import com.tngtech.jgiven.Stage;3import com.tngtech.jgiven.annotation.ProvidedScenarioState;4import com.tngtech.jgiven.examples.coffeemachine.CoffeeMachine;5public class WhenCoffee extends Stage<WhenCoffee> {6 CoffeeMachine coffeeMachine = new CoffeeMachine();7 public WhenCoffee I_press_the_coffee_button() {8 coffeeMachine.pressCoffeeButton();9 return self();10 }11}12package com.tngtech.jgiven.examples.coffeemachine.steps;13import com.tngtech.jgiven.Stage;14import com.tngtech.jgiven.annotation.ProvidedScenarioState;15import com.tngtech.jgiven.examples.coffeemachine.CoffeeMachine;16public class GivenCoffee extends Stage<GivenCoffee> {17 CoffeeMachine coffeeMachine = new CoffeeMachine();18 public GivenCoffee coffee_machine_is_started() {19 coffeeMachine.start();20 return self();21 }22}23package com.tngtech.jgiven.examples.coffeemachine.steps;24import com.tngtech.jgiven.Stage;25import com.tngtech.jgiven.annotation.ProvidedScenarioState;26import com.tngtech.jgiven.examples.coffeemachine.CoffeeMachine;27public class ThenCoffee extends Stage<ThenCoffee> {28 CoffeeMachine coffeeMachine;29 public ThenCoffee coffee_should_be_served() {30 assertThat( coffeeMachine.isCoffeeServed() ).isTrue();31 return self();32 }33}34package com.tngtech.jgiven.examples.coffeemachine.steps;35import com.tngtech.jgiven.Stage;36import com.tngtech.jgiven.annotation.ProvidedScenarioState;37import com.tngtech.jgiven.examples.coffeemachine.CoffeeMachine;38public class ThenCoffee extends Stage<ThenCoffee> {39 CoffeeMachine coffeeMachine;
I_press_the_coffee_button
Using AI Code Generation
1When().I_press_the_coffee_button();2Then().coffee_should_be_served();3Then().coffee_machine_should_display_message( "Your coffee is ready" );4When().I_press_the_coffee_button();5Then().coffee_should_be_served();6Then().coffee_machine_should_display_message( "Your coffee is ready" );7When().I_press_the_coffee_button();8Then().coffee_should_be_served();9Then().coffee_machine_should_display_message( "Your coffee is ready" );10When().I_press_the_coffee_button();11Then().coffee_should_be_served();12Then().coffee_machine_should_display_message( "Your coffee is ready" );
I_press_the_coffee_button
Using AI Code Generation
1When().I_press_the_coffee_button();2Then().the_coffee_should_be_served();3}4public void a_coffee_can_be_served() {5 Given().the_coffee_machine_is_started();6 When().I_press_the_coffee_button();7 Then().the_coffee_should_be_served();8}9public void a_coffee_can_be_served_with_a_sugar_cube() {10 Given().the_coffee_machine_is_started();11 When().I_press_the_coffee_button();12 Then().the_coffee_should_be_served();13 Then().a_sugar_cube_should_be_served();14}15public void a_coffee_can_be_served_with_a_sugar_cube() {16 Given().the_coffee_machine_is_started();17 When().I_press_the_coffee_button();18 Then().the_coffee_should_be_served();19 Then().a_sugar_cube_should_be_served();20}21public void a_coffee_can_be_served_with_a_sugar_cube_and_a_spoon() {22 Given().the_coffee_machine_is_started();23 When().I_press_the_coffee_button();24 Then().the_coffee_should_be_served();25 Then().a_sugar_cube_should_be_served();26 Then().a_spoon_should_be_served();27}28public void a_coffee_can_be_served_with_a_sugar_cube_and_a_spoon() {29 Given().the_coffee_machine_is_started();30 When().I_press_the_coffee_button();31 Then().the_coffee_should_be_served();32 Then().a_sugar_cube_should_be_served();33 Then().a_spoon_should_be_served();34 Then().the_coffee_machine_is_empty();35}
I_press_the_coffee_button
Using AI Code Generation
1When().I_press_the_coffee_button();2Then().I_should_have_coffee();3When().I_press_the_coffee_button();4Then().I_should_have_coffee();5When().I_press_the_coffee_button();6Then().I_should_have_coffee();7When().I_press_the_coffee_button();8Then().I_should_have_coffee();9When().I_press_the_coffee_button();10Then().I_should_have_coffee();11When().I_press_the_coffee_button();12Then().I_should_have_coffee();13When().I_press_the_coffee_button();14Then().I_should_have_coffee();
I_press_the_coffee_button
Using AI Code Generation
1Given().I_press_the_coffee_button();2When().I_press_the_coffee_button();3Then().I_press_the_coffee_button();4And().I_press_the_coffee_button();5But().I_press_the_coffee_button();6When().I_press_the_coffee_button();7Then().I_press_the_coffee_button();8And().I_press_the_coffee_button();9But().I_press_the_coffee_button();10When().I_press_the_coffee_button();11Then().I_press_the_coffee_button();12And().I_press_the_coffee_button();13But().I_press_the_coffee_button();14When().I_press_the_coffee_button();
I_press_the_coffee_button
Using AI Code Generation
1When().I_press_the_coffee_button();2Then().button_should_be_pressed();3Then().coffee_should_be_served();4Then().the_coffee_machine_should_be_ready_again();5Then().the_coffee_machine_should_be_ready_again();6Then().the_coffee_machine_should_be_ready_again();7}8}
I_press_the_coffee_button
Using AI Code Generation
1 public WhenCoffee I_press_the_coffee_button() {2 return coffeeMachine.pressCoffeeButton();3 }4 public WhenCoffee I_press_the_coffee_button() {5 return coffeeMachine.pressCoffeeButton();6 }7 public WhenCoffee I_press_the_coffee_button() {8 return coffeeMachine.pressCoffeeButton();9 }10 public WhenCoffee I_press_the_coffee_button() {11 return coffeeMachine.pressCoffeeButton();12 }13 public WhenCoffee I_press_the_coffee_button() {14 return coffeeMachine.pressCoffeeButton();15 }16 public WhenCoffee I_press_the_coffee_button() {17 return coffeeMachine.pressCoffeeButton();18 }19 public WhenCoffee I_press_the_coffee_button() {20 return coffeeMachine.pressCoffeeButton();21 }22 public WhenCoffee I_press_the_coffee_button() {23 return coffeeMachine.pressCoffeeButton();24 }
I_press_the_coffee_button
Using AI Code Generation
1When().I_press_the_coffee_button();2Then().button_should_be_pressed();3Then().coffee_should_be_served();4Then().the_coffee_machine_should_be_ready_again();5Then().the_coffee_machine_should_be_ready_again();6Then().the_coffee_machine_should_be_ready_again();7}8}
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!!