まずは、どう作るかは別にして、最終的にテストをできる形になったものを示す
package com.mycompany.cucumberexam; import lombok.Data; @Data public class App { private int stock; public boolean order(int amount) { if (amount <= stock) { stock = stock - amount; return true; } return false; } }
#language: ja フィーチャ: 受注業務 シナリオ: 受注成立 前提 在庫数が 20 である もし 10 個注文する ならば 受注が成立する ならば 在庫数が 10 になる シナリオ: 受注失敗 前提 在庫数が 10 である もし 20 個注文する ならば 受注が失敗する ならば 在庫数が 10 になる
#language: ja フィーチャ: 受注業務 シナリオ: 受注成立 前提 在庫数が 20 である もし 10 個注文する ならば 受注が成立する かつ 在庫数が 10 になる シナリオ: 受注失敗 前提 在庫数が 10 である もし 20 個注文する ならば 受注が失敗する かつ 在庫数が 10 になる
package com.mycompany.cucumberexam; import cucumber.api.CucumberOptions; import cucumber.api.junit.Cucumber; import org.junit.runner.RunWith; @RunWith(Cucumber.class) @CucumberOptions( features = {"src/test/resources/features/example.feature"}, format = {"pretty"}, monochrome = true ) public class AppTest { }
CucumberOptions で glue = { "com.mycompany.cucumberexam.stepdefs" }を指定する
features = {"classpath:features/example.feature"},
package com.mycompany.cucumberexam; import cucumber.api.java.ja.ならば; import cucumber.api.java.ja.もし; import cucumber.api.java.ja.前提; import static org.junit.Assert.assertThat; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.equalTo; public class ExampleStepDefs { private App app = new App(); private boolean result; @前提("^在庫数が (\\d+) である$") public void 在庫数が_である(int arg1) throws Throwable { app.setStock(arg1); } @もし("^(\\d+) 個注文する$") public void 個注文する(int arg1) throws Throwable { result = app.order(arg1); } @ならば("^受注が成立する$") public void 受注が成立する() throws Throwable { assertThat(result, is(equalTo(true))); } @ならば("^在庫数が (\\d+) になる$") public void 在庫数が_になる(int arg1) throws Throwable { assertThat(app.getStock(), is(equalTo(arg1))); } @ならば("^受注が失敗する$") public void 受注が失敗する() throws Throwable { assertThat(result, is(equalTo(false))); } }
<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> <groupId>com.mycompany</groupId> <artifactId>CucumberExam</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>CucumberExam</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-java</artifactId> <version>1.1.5</version> <scope>test</scope> </dependency> <dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-junit</artifactId> <version>1.1.5</version> <scope>test</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.12.4</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.16</version> </plugin> </plugins> </build> </project>
------------------------------------------------------------------------ Building CucumberExam 1.0-SNAPSHOT ------------------------------------------------------------------------ --- maven-surefire-plugin:2.16:test (default-cli) @ CucumberExam --- Surefire report directory: /Users/atsushi/NetBeansProjects/CucumberExam/target/surefire-reports ------------------------------------------------------- T E S T S ------------------------------------------------------- Running com.mycompany.cucumberexam.AppTest #language: ja フィーチャ: 受注業務 シナリオ: 受注成立 # src/test/resources/features/example.feature:3 前提在庫数が 20 である もし10 個注文する ならば受注が成立する ならば在庫数が 10 になる シナリオ: 受注失敗 # src/test/resources/features/example.feature:8 前提在庫数が 10 である もし20 個注文する ならば受注が失敗する ならば在庫数が 10 になる 2 Scenarios (2 undefined) 8 Steps (8 undefined) 0m0.000s You can implement missing steps with the snippets below: @前提("^在庫数が (\\d+) である$") public void 在庫数が_である(int arg1) throws Throwable { // Express the Regexp above with the code you wish you had throw new PendingException(); } @もし("^(\\d+) 個注文する$") public void 個注文する(int arg1) throws Throwable { // Express the Regexp above with the code you wish you had throw new PendingException(); } @ならば("^受注が成立する$") public void 受注が成立する() throws Throwable { // Express the Regexp above with the code you wish you had throw new PendingException(); } @ならば("^在庫数が (\\d+) になる$") public void 在庫数が_になる(int arg1) throws Throwable { // Express the Regexp above with the code you wish you had throw new PendingException(); } @ならば("^受注が失敗する$") public void 受注が失敗する() throws Throwable { // Express the Regexp above with the code you wish you had throw new PendingException(); } Tests run: 12, Failures: 0, Errors: 0, Skipped: 10, Time elapsed: 0.328 sec - in com.mycompany.cucumberexam.AppTest Results : Tests run: 12, Failures: 0, Errors: 0, Skipped: 10 ------------------------------------------------------------------------ BUILD SUCCESS ------------------------------------------------------------------------ Total time: 1.269s Finished at: Fri Mar 07 01:40:19 JST 2014 Final Memory: 8M/310M ------------------------------------------------------------------------
#language: ja フィーチャ: 受注業務 シナリオ: 受注成立 前提 在庫数が 20 である もし 10 個注文する ならば 受注が成立する ならば 在庫数が 10 になる シナリオ: 受注失敗 前提 在庫数が 10 である もし 20 個注文する ならば 受注が失敗する ならば 在庫数が 10 になる
#language: ja フィーチャ: 受注業務 シナリオテンプレート: 受注 前提 在庫数が <処理前在庫> である もし <注文> 個注文する ならば 受注が<注文結果>する ならば 在庫数が <処理後在庫> になる 例: |処理前在庫|注文|注文結果|処理後在庫| |20|10|成立|10| |10|20|失敗|10|