forked from cucumber/cucumber-jvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Java Calculator TestNG example project.
- Loading branch information
Dmytro Chyzhykov
committed
Sep 1, 2013
1 parent
37d3185
commit b5a3bc5
Showing
13 changed files
with
324 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/.settings | ||
/.classpath | ||
/.project |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
This is a TestNG copy-paste version of the [JUnit Calculator example](https://github.com/cucumber/cucumber-jvm/tree/master/examples/java-calculator) project. | ||
|
||
If you find its TestNG report is not idiomatic, consider making a contribution to improve Cucumber JVM TestNG Support. | ||
|
||
**Note** | ||
Please keep code base of this project up-to-date | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>info.cukes</groupId> | ||
<artifactId>cucumber-jvm</artifactId> | ||
<relativePath>../../pom.xml</relativePath> | ||
<version>1.1.5-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>java-calculator-testng</artifactId> | ||
<packaging>jar</packaging> | ||
<name>Examples: Java Calculator TestNG</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>info.cukes</groupId> | ||
<artifactId>cucumber-jvm-deps</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>info.cukes</groupId> | ||
<artifactId>cucumber-java</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>info.cukes</groupId> | ||
<artifactId>cucumber-testng</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
15 changes: 15 additions & 0 deletions
15
...ava-calculator-testng/src/main/java/cucumber/examples/java/calculator/DateCalculator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package cucumber.examples.java.calculator; | ||
|
||
import java.util.Date; | ||
|
||
public class DateCalculator { | ||
private Date now; | ||
|
||
public DateCalculator(Date now) { | ||
this.now = now; | ||
} | ||
|
||
public String isDateInThePast(Date date) { | ||
return (date.before(now)) ? "yes" : "no"; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...java-calculator-testng/src/main/java/cucumber/examples/java/calculator/RpnCalculator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package cucumber.examples.java.calculator; | ||
|
||
import java.util.Deque; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
import static java.util.Arrays.asList; | ||
|
||
public class RpnCalculator { | ||
private final Deque<Number> stack = new LinkedList<Number>(); | ||
private static final List<String> OPS = asList("-", "+", "*", "/"); | ||
|
||
public void push(Object arg) { | ||
if (OPS.contains(arg)) { | ||
Number y = stack.removeLast(); | ||
Number x = stack.isEmpty() ? 0 : stack.removeLast(); | ||
Double val = null; | ||
if (arg.equals("-")) { | ||
val = x.doubleValue() - y.doubleValue(); | ||
} else if (arg.equals("+")) { | ||
val = x.doubleValue() + y.doubleValue(); | ||
} else if (arg.equals("*")) { | ||
val = x.doubleValue() * y.doubleValue(); | ||
} else if (arg.equals("/")) { | ||
val = x.doubleValue() / y.doubleValue(); | ||
} | ||
push(val); | ||
} else { | ||
stack.add((Number) arg); | ||
} | ||
} | ||
|
||
public void PI() { | ||
push(Math.PI); | ||
} | ||
|
||
public Number value() { | ||
return stack.getLast(); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
.../java-calculator-testng/src/test/java/cucumber/examples/java/calculator/DateStepdefs.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package cucumber.examples.java.calculator; | ||
|
||
import cucumber.api.Format; | ||
import cucumber.api.java.en.Given; | ||
import cucumber.api.java.en.Then; | ||
import cucumber.api.java.en.When; | ||
|
||
import java.util.Date; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class DateStepdefs { | ||
private String result; | ||
private DateCalculator calculator; | ||
|
||
@Given("^today is (.+)$") | ||
public void today_is(@Format("yyyy-MM-dd") Date date) { | ||
calculator = new DateCalculator(date); | ||
} | ||
|
||
/** | ||
* We don't need to use @Format here, since the date string in the step | ||
* conforms to <code>SimpleDateFormat.SHORT</code>. Cucumber has built-in support for | ||
* <code>SimpleDateFormat.SHORT</code>, <code>SimpleDateFormat.MEDIUM</code>, | ||
* <code>SimpleDateFormat.LONG</code> and <code>SimpleDateFormat.FULL</code>. | ||
* | ||
* @see <a href="http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html">SimpleDateFormat</a> | ||
*/ | ||
@When("^I ask if (.+) is in the past$") | ||
public void I_ask_if_date_is_in_the_past(Date date) { | ||
result = calculator.isDateInThePast(date); | ||
} | ||
|
||
@Then("^the result should be (.+)$") | ||
public void the_result_should_be(String expectedResult) { | ||
assertEquals(expectedResult, result); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...culator-testng/src/test/java/cucumber/examples/java/calculator/RpnCalculatorStepdefs.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package cucumber.examples.java.calculator; | ||
|
||
import cucumber.api.Scenario; | ||
import cucumber.api.java.After; | ||
import cucumber.api.java.Before; | ||
import cucumber.api.java.en.Given; | ||
import cucumber.api.java.en.Then; | ||
import cucumber.api.java.en.When; | ||
|
||
import java.util.List; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class RpnCalculatorStepdefs { | ||
private RpnCalculator calc; | ||
|
||
@Given("^a calculator I just turned on$") | ||
public void a_calculator_I_just_turned_on() { | ||
calc = new RpnCalculator(); | ||
} | ||
|
||
@When("^I add (\\d+) and (\\d+)$") | ||
public void adding(int arg1, int arg2) { | ||
calc.push(arg1); | ||
calc.push(arg2); | ||
calc.push("+"); | ||
} | ||
|
||
@Given("^I press (.+)$") | ||
public void I_press(String what) { | ||
calc.push(what); | ||
} | ||
|
||
@Then("^the result is (\\d+)$") | ||
public void the_result_is(double expected) { | ||
assertEquals(expected, calc.value()); | ||
} | ||
|
||
@Before({"~@foo"}) | ||
public void before() { | ||
System.out.println("Runs before scenarios *not* tagged with @foo"); | ||
} | ||
|
||
@After | ||
public void after(Scenario scenario) { | ||
// result.write("HELLLLOO"); | ||
} | ||
|
||
@Given("^the previous entries:$") | ||
public void thePreviousEntries(List<Entry> entries) { | ||
for (Entry entry : entries) { | ||
calc.push(entry.first); | ||
calc.push(entry.second); | ||
calc.push(entry.operation); | ||
} | ||
} | ||
|
||
public class Entry { | ||
Integer first; | ||
Integer second; | ||
String operation; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
.../java-calculator-testng/src/test/java/cucumber/examples/java/calculator/RunCukesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package cucumber.examples.java.calculator; | ||
|
||
import cucumber.api.CucumberOptions; | ||
import cucumber.api.testng.AbstractTestNGCucumberTests; | ||
|
||
@CucumberOptions(format = "json:target/cucumber-report.json") | ||
public class RunCukesTest extends AbstractTestNGCucumberTests { | ||
} |
59 changes: 59 additions & 0 deletions
59
...a-calculator-testng/src/test/java/cucumber/examples/java/calculator/ShoppingStepdefs.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package cucumber.examples.java.calculator; | ||
|
||
import cucumber.api.Transformer; | ||
import cucumber.api.java.en.Given; | ||
import cucumber.api.java.en.Then; | ||
import cucumber.api.java.en.When; | ||
import cucumber.deps.com.thoughtworks.xstream.annotations.XStreamConverter; | ||
|
||
import java.util.List; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class ShoppingStepdefs { | ||
private RpnCalculator calc = new RpnCalculator(); | ||
|
||
@Given("^the following groceries:$") | ||
public void the_following_groceries(List<Grocery> groceries) { | ||
for (Grocery grocery : groceries) { | ||
calc.push(grocery.price.value); | ||
calc.push("+"); | ||
} | ||
} | ||
|
||
@When("^I pay (\\d+)$") | ||
public void i_pay(int amount) { | ||
calc.push(amount); | ||
calc.push("-"); | ||
} | ||
|
||
@Then("^my change should be (\\d+)$") | ||
public void my_change_should_be_(int change) { | ||
assertEquals(-calc.value().intValue(), change); | ||
} | ||
|
||
public static class Grocery { | ||
public String name; | ||
@XStreamConverter(Price.Converter.class) | ||
public Price price; | ||
|
||
public Grocery() { | ||
super(); | ||
} | ||
} | ||
|
||
public static class Price { | ||
public int value; | ||
|
||
public Price(int value) { | ||
this.value = value; | ||
} | ||
|
||
public static class Converter extends Transformer<Price> { | ||
@Override | ||
public Price transform(String value) { | ||
return new Price(Integer.parseInt(value)); | ||
} | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...ator-testng/src/test/resources/cucumber/examples/java/calculator/basic_arithmetic.feature
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
@foo | ||
Feature: Basic Arithmetic | ||
|
||
Background: A Calculator | ||
Given a calculator I just turned on | ||
|
||
Scenario: Addition | ||
# Try to change one of the values below to provoke a failure | ||
When I add 4 and 5 | ||
Then the result is 9 | ||
|
||
Scenario: Another Addition | ||
# Try to change one of the values below to provoke a failure | ||
When I add 4 and 7 | ||
Then the result is 11 | ||
|
||
Scenario Outline: Many additions | ||
Given the previous entries: | ||
| first | second | operation | | ||
| 1 | 1 | + | | ||
| 2 | 1 | + | | ||
When I press + | ||
And I add <a> and <b> | ||
And I press + | ||
Then the result is <c> | ||
|
||
Examples: Single digits | ||
| a | b | c | | ||
| 1 | 2 | 8 | | ||
| 2 | 3 | 10 | | ||
|
||
Examples: Double digits | ||
| a | b | c | | ||
| 10 | 20 | 35 | | ||
| 20 | 30 | 55 | |
8 changes: 8 additions & 0 deletions
8
...lator-testng/src/test/resources/cucumber/examples/java/calculator/date_calculator.feature
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
Feature: Dates with different date formats | ||
This feature shows you can have different date formats, as long as you annotate the | ||
corresponding step definition method accordingly. | ||
|
||
Scenario: Determine past date | ||
Given today is 2011-01-20 | ||
When I ask if Jan 19, 2011 is in the past | ||
Then the result should be yes |
10 changes: 10 additions & 0 deletions
10
...a-calculator-testng/src/test/resources/cucumber/examples/java/calculator/shopping.feature
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
Feature: Shopping | ||
|
||
Scenario: Give correct change | ||
Given the following groceries: | ||
| name | price | | ||
| milk | 9 | | ||
| bread | 7 | | ||
| soap | 5 | | ||
When I pay 25 | ||
Then my change should be 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters