From d03b5c03175b8d054f82e48945146d6a8f299e9c Mon Sep 17 00:00:00 2001 From: Kingsley Hendrickse Date: Sat, 14 Apr 2012 09:00:18 +0100 Subject: [PATCH] first commit --- .gitignore | 5 ++ README.md | 13 ++++ cucumber-jvm-example.iml | 33 ++++++++ pom.xml | 77 +++++++++++++++++++ .../java/net/masterthought/example/ATM.java | 23 ++++++ .../net/masterthought/example/Account.java | 24 ++++++ .../net/masterthought/example/CreditCard.java | 34 ++++++++ .../masterthought/example/ATMScenario.java | 57 ++++++++++++++ .../net/masterthought/example/ATMTest.java | 11 +++ .../net/masterthought/example/ATM.feature | 29 +++++++ .../net/masterthought/example/ATMK.feature | 18 +++++ 11 files changed, 324 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 cucumber-jvm-example.iml create mode 100644 pom.xml create mode 100644 src/main/java/net/masterthought/example/ATM.java create mode 100644 src/main/java/net/masterthought/example/Account.java create mode 100644 src/main/java/net/masterthought/example/CreditCard.java create mode 100644 src/test/java/net/masterthought/example/ATMScenario.java create mode 100644 src/test/java/net/masterthought/example/ATMTest.java create mode 100644 src/test/resources/net/masterthought/example/ATM.feature create mode 100644 src/test/resources/net/masterthought/example/ATMK.feature diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..85ea697 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +/.project +/.classpath +/.settings +/target +.idea diff --git a/README.md b/README.md new file mode 100644 index 0000000..823e3dd --- /dev/null +++ b/README.md @@ -0,0 +1,13 @@ +# An Example [cucumber-jvm](https://github.com/cucumber/cucumber-jvm) Project using Maven +This is an example maven cucumber jvm project that uses maven to build and run some simple scenarios based around withdrawing cash from an ATM. + +## Background + +Cucumber-JVM is a test automation tool following the principles of Behavioural Driven Design and living documentation. Specifications are written in a concise human readable form and executed in continuous integration. + +## Use + +After you have installed maven: mvn clean install will run the tests and produce the test output. There are 3 report formatters - 1 on the console which prints pretty results to the console, there is an html report which gets generated in the target/cucumber directory and finally there is a cucumber.json report which gets published to the target directory. + +Some of the tests pass and some fail to illustrate how they look. + diff --git a/cucumber-jvm-example.iml b/cucumber-jvm-example.iml new file mode 100644 index 0000000..c19cdb8 --- /dev/null +++ b/cucumber-jvm-example.iml @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..9ba3c1b --- /dev/null +++ b/pom.xml @@ -0,0 +1,77 @@ + + 4.0.0 + net.masterthought.example + cucumber-jvm-example + 0.0.1-SNAPSHOT + + + UTF-8 + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.6 + 1.6 + + + + + + + + + + + junit + junit + 4.10 + test + + + + info.cukes + cucumber-junit + 1.0.2 + test + + + info.cukes + cucumber-html + 0.2.1 + test + + + info.cukes + cucumber-java + 1.0.2 + test + + + info.cukes + cucumber-picocontainer + 1.0.2 + test + + + org.picocontainer + picocontainer + 2.10.2 + test + + + + + + sonatype-snapshots + https://oss.sonatype.org/content/repositories/snapshots + + + + + + \ No newline at end of file diff --git a/src/main/java/net/masterthought/example/ATM.java b/src/main/java/net/masterthought/example/ATM.java new file mode 100644 index 0000000..43a6052 --- /dev/null +++ b/src/main/java/net/masterthought/example/ATM.java @@ -0,0 +1,23 @@ +package net.masterthought.example; + +public class ATM { + + private int money; + + public ATM(int money) { + this.money = money; + } + + public int requestMoney(CreditCard creditCard, int amount) { + if (!creditCard.isValid() || amount > money) { + return 0; + } + + amount = creditCard.getAccount().getMoney(amount); + + money = money - amount; + + return amount; + } + +} diff --git a/src/main/java/net/masterthought/example/Account.java b/src/main/java/net/masterthought/example/Account.java new file mode 100644 index 0000000..8932ef3 --- /dev/null +++ b/src/main/java/net/masterthought/example/Account.java @@ -0,0 +1,24 @@ +package net.masterthought.example; + +public class Account { + + private int balance; + + public Account(int balance) { + this.balance = balance; + } + + public int getBalance() { + return balance; + } + + public int getMoney(int amount) { + if (balance < amount) + return 0; + + balance = balance - amount; + + return amount; + + } +} diff --git a/src/main/java/net/masterthought/example/CreditCard.java b/src/main/java/net/masterthought/example/CreditCard.java new file mode 100644 index 0000000..2fafe65 --- /dev/null +++ b/src/main/java/net/masterthought/example/CreditCard.java @@ -0,0 +1,34 @@ +package net.masterthought.example; + +public class CreditCard { + + private boolean valid = true; + private boolean inUse = false; + + private final Account account; + + public CreditCard(Account account) { + this.account = account; + } + + public boolean isValid() { + return valid; + } + + public Account getAccount() { + return account; + } + + public void insertCard(){ + inUse = true; + } + + public void returnCard(){ + inUse = false; + } + + public boolean isInUse() { + return inUse; + } + +} diff --git a/src/test/java/net/masterthought/example/ATMScenario.java b/src/test/java/net/masterthought/example/ATMScenario.java new file mode 100644 index 0000000..dc1f2a5 --- /dev/null +++ b/src/test/java/net/masterthought/example/ATMScenario.java @@ -0,0 +1,57 @@ +package net.masterthought.example; + +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; +import cucumber.annotation.en.And; +import cucumber.annotation.en.Given; +import cucumber.annotation.en.Then; +import cucumber.annotation.en.When; +import net.masterthought.example.ATM; +import net.masterthought.example.Account; +import net.masterthought.example.CreditCard; + +public class ATMScenario { + + private ATM atm; + private Account account; + private CreditCard creditCard; + + private int money; + + @Given("^the account balance is (\\d*)$") + public void createAccount(int balance) { + account = new Account(balance); + } + + @And("^the card is valid$") + public void createCreditCard() { + creditCard = new CreditCard(account); + } + + @And("^the machine contains (\\d*)$") + public void createATM(int money) { + atm = new ATM(money); + } + + @When("^the Account Holder requests (\\d*)$") + public void requestMoney(int amount) { + money = atm.requestMoney(creditCard, amount); + } + + @Then("^the ATM should dispense (\\d*)$") + public void checkMoney(int amount) { + assertThat(money, is(amount)); + } + + @And("^the account balance should be (\\d*)$") + public void checkBalance(int newBalance) { + assertThat(newBalance, is(creditCard.getAccount().getBalance())); + } + + @And("^the card should be returned$") + public void cardShouldBeReturned() { + assertFalse(creditCard.isInUse()); + } + +} diff --git a/src/test/java/net/masterthought/example/ATMTest.java b/src/test/java/net/masterthought/example/ATMTest.java new file mode 100644 index 0000000..f6aa007 --- /dev/null +++ b/src/test/java/net/masterthought/example/ATMTest.java @@ -0,0 +1,11 @@ +package net.masterthought.example; + +import cucumber.junit.Cucumber; +import org.junit.runner.RunWith; + +@RunWith(Cucumber.class) +@Cucumber.Options(format = {"pretty", "html:target/cucumber", "json:target/cucumber.json"}) +public class ATMTest { + + +} diff --git a/src/test/resources/net/masterthought/example/ATM.feature b/src/test/resources/net/masterthought/example/ATM.feature new file mode 100644 index 0000000..a6d138e --- /dev/null +++ b/src/test/resources/net/masterthought/example/ATM.feature @@ -0,0 +1,29 @@ +Feature: Account Holder withdraws cash + As an Account Holder + I want to withdraw cash from an ATM + So that I can get money when the bank is closed + + @myone + Scenario Outline: Account has sufficient funds + Given the account balance is + And the card is valid + And the machine contains + When the Account Holder requests + Then the ATM should dispense + And the account balance should be + And the card should be returned + + Examples: + |account_balance|atm_available|request|result|newBalance| + |1004 |100 |20 |20 |80 | + |1001 |100 |20 |20 |80 | + + Scenario: Account has sufficient funds again + Given the account balance is 300 + And the card is valid + And the machine contains 300 + When the Account Holder requests 20 + Then the ATM should dispense 20 + And the account balance should be 280 + And the card should be returned + diff --git a/src/test/resources/net/masterthought/example/ATMK.feature b/src/test/resources/net/masterthought/example/ATMK.feature new file mode 100644 index 0000000..04f996f --- /dev/null +++ b/src/test/resources/net/masterthought/example/ATMK.feature @@ -0,0 +1,18 @@ +Feature: Account Holder withdraws cash + As an Account Holder + I want to withdraw cash from an ATM + So that I can get money when the bank is closed + + Scenario Outline: Account has sufficient funds + Given the account balance is + And the card is valid + And the machine contains + When the Account Holder requests + Then the ATM should dispense + And the account balance should be + And the card should be returned + + Examples: + |account_balance|atm_available|request|result|newBalance| + |100 |100 |20 |20 |80 | +