Best JGiven code snippet using com.tngtech.jgiven.examples.coffeemachine.steps.GivenCoffee.a_coffee_machine
Source:ServeCoffeeTest.java
...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" )183 public void coffe_making_gets_better( String description, int runNr, String result ) {184 given().a_coffee_machine();185 when().I_make_coffee_for_the_$_time( runNr );186 then().the_result_is( result );187 }188 // end::casedescription[]189 @Test190 @FailingOnPurpose191 public void long_error_messages_should_wrapped() {192 given().an_exception_with_a_very_long_message();193 }194}...
Source:GivenCoffee.java
...11 @ProvidedScenarioState12 private int euros;13 @ExtendedDescription( "An empty coffee machine that is already turned on.<br>"14 + "The coffee price is set to 2 EUR." )15 public GivenCoffee a_coffee_machine() {16 coffeeMachine = new CoffeeMachine();17 coffeeMachine.on = true;18 return this;19 }20 @ExtendedDescription( "The number of coffees in the machine is set to the given value." )21 public GivenCoffee there_are_$_coffees_left_in_the_machine( int coffees ) {22 coffeeMachine.coffees = coffees;23 return this;24 }25 public GivenCoffee the_coffee_costs_$_euros( int price ) {26 coffeeMachine.price = price;27 return this;28 }29 public GivenCoffee the_machine_is_$onOrOff( @Format( value = BooleanFormatter.class, args = { "on", "off" } ) boolean onOrOff ) {30 coffeeMachine.on = onOrOff;31 return this;32 }33 public GivenCoffee there_are_no_more_coffees_left() {34 return there_are_$_coffees_left_in_the_machine( 0 );35 }36 public GivenCoffee the_machine_is_turned_off() {37 coffeeMachine.on = false;38 return this;39 }40 public GivenCoffee an_empty_coffee_machine() {41 return a_coffee_machine()42 .and().there_are_no_more_coffees_left();43 }44 public GivenCoffee an_exception_with_a_very_long_message() {45 throw new RuntimeException("This is a very long exception message that should be wrapped at some point " +46 "in the report and it is even longer than that");47 }48}...
Source:JUnitParamsServeCoffeeTest.java
...24 "true, 0, 2, false",25 "false, 1, 2, false"26 } )27 public void buy_a_coffee( boolean onOrOff, int coffees, int dollars, boolean shouldOrShouldNot ) {28 given().a_coffee_machine().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}...
a_coffee_machine
Using AI Code Generation
1package com.tngtech.jgiven.examples.coffeemachine;2import com.tngtech.jgiven.Stage;3import com.tngtech.jgiven.annotation.ProvidedScenarioState;4import com.tngtech.jgiven.examples.coffeemachine.steps.GivenCoffee;5import com.tngtech.jgiven.examples.coffeemachine.steps.ThenCoffee;6import com.tngtech.jgiven.examples.coffeemachine.steps.WhenCoffee;7public class CoffeeMachineStage extends Stage<CoffeeMachineStage> {8 WhenCoffee when = new WhenCoffee();9 ThenCoffee then = new ThenCoffee();10 GivenCoffee given = new GivenCoffee();11 public void a_coffee_machine() {12 given.a_coffee_machine();13 }14 public void I_press_the_button() {15 when.I_press_the_button();16 }17 public void coffee_should_be_served() {18 then.coffee_should_be_served();19 }20}21package com.tngtech.jgiven.examples.coffeemachine;22import com.tngtech.jgiven.Stage;23import com.tngtech.jgiven.annotation.ProvidedScenarioState;24import com.tngtech.jgiven.examples.coffeemachine.steps.GivenCoffee;25import com.tngtech.jgiven.examples.coffeemachine.steps.ThenCoffee;26import com.tngtech.jgiven.examples.coffeemachine.steps.WhenCoffee;27public class CoffeeMachineStage extends Stage<CoffeeMachineStage> {28 WhenCoffee when = new WhenCoffee();29 ThenCoffee then = new ThenCoffee();30 GivenCoffee given = new GivenCoffee();31 public void a_coffee_machine() {32 given.a_coffee_machine();33 }34 public void I_press_the_button() {35 when.I_press_the_button();36 }37 public void coffee_should_be_served() {38 then.coffee_should_be_served();39 }40}41package com.tngtech.jgiven.examples.coffeemachine;42import com.tngtech.jgiven.Stage;43import com
a_coffee_machine
Using AI Code Generation
1package com.tngtech.jgiven.examples.coffeemachine;2import com.tngtech.jgiven.Stage;3import com.tngtech.jgiven.annotation.ExpectedScenarioState;4import com.tngtech.jgiven.annotation.Quoted;5import com.tngtech.jgiven.annotation.ScenarioState;6import com.tngtech.jgiven.annotation.ScenarioState.Resolution;7import com.tngtech.jgiven.examples.coffeemachine.CoffeeMachine.State;8public class GivenCoffee extends Stage<GivenCoffee> {9 @ScenarioState(resolution = Resolution.NAME)10 CoffeeMachine coffeeMachine;11 public GivenCoffee a_coffee_machine() {
a_coffee_machine
Using AI Code Generation
1Given().a_coffee_machine();2Then().coffee_is_served();3Then().coffee_is_not_served();4When().the_coffee_machine_is_started();5When().the_coffee_machine_is_stopped();6When().the_coffee_machine_is_told_to_make_coffee();7When().the_coffee_machine_is_told_to_make_coffee_with_a_sugar_cube();8When().the_coffee_machine_is_told_to_make_coffee_with_$_sugar_cubes(0);9When().the_coffee_machine_is_told_to_make_coffee_with_$_sugar_cubes(1);10When().the_coffee_machine_is_told_to_make_coffee_with_$_sugar_cubes(2);11When().the_coffee_machine_is_told_to_make
a_coffee_machine
Using AI Code Generation
1Given().a_coffee_machine();2When().user_starts_coffee();3Then().coffee_should_be_served();4Then().coffee_machine_should_be_empty();5Then().coffee_machine_should_be_empty();6Then().coffee_should_be_served();7Then().coffee_machine_should_be_empty();8Then().coffee_should_be_served();9Then().coffee_machine_should_be_empty();10Then().coffee_should_be_served();11Then().coffee_machine_should_be_empty();12Then().coffee_should_be_served();13Then().coffee_machine_should_be_empty();14Then().coffee_should_be_served();
a_coffee_machine
Using AI Code Generation
1Given().a_coffee_machine();2Then().coffee_is_served();3When().serve_coffee();4Given().a_coffee_machine();5Then().coffee_is_served();6When().serve_coffee();7Given().a_coffee_machine();8Then().coffee_is_served();9When().serve_coffee();10Given().a_coffee_machine();11Then().coffee_is_served();12When().serve_coffee();13Given().a_coffee_machine();14Then().coffee_is_served();15When().serve
a_coffee_machine
Using AI Code Generation
1Given().a_coffee_machine();2When().I_have_coffee();3Then().I_should_have_coffee();4Then().I_should_have_coffee();5Then().I_should_have_coffee();6Then().I_should_have_coffee();7Then().I_should_have_coffee();8Then().I_should_have_coffee();9Then().I_should_have_coffee();10Then().I_should_have_coffee();11Then().I_should_have_coffee();12Then().I_should_have_coffee();13Then().I_should_have_coffee();14Then().I_should_have_coffee();15Then().I_should_have_coffee();
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!!