キュウリの春の統合
1. 概要
Cucumberは、Rubyプログラミング言語で記述された非常に強力なテストフレームワークであり、BDD(ビヘイビア駆動開発)方法論に従います。 これにより、開発者は、技術者以外の利害関係者が検証できるプレーンテキストで高レベルのユースケースを記述し、Gherkinと呼ばれる言語で記述された実行可能テストに変換できます。
これらについては、別の記事ですでに説明しています。
また、 Cucumber-Spring Integration は、テストの自動化を容易にすることを目的としています。 CucumberテストをSpringと統合すると、Mavenビルドと一緒にそれらを実行できるようになります。
2. Mavenの依存関係
Cucumber-JVM依存関係から始めて、Maven依存関係を定義することにより、Cucumber-Spring統合の使用を開始しましょう。
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>6.8.0</version>
<scope>test</scope>
</dependency>
CucumberJVMの最新バージョンはここにあります。
次に、JUnitとCucumberのテスト依存関係を追加します。
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>6.8.0</version>
<scope>test</scope>
</dependency>
Cucumber JUnitの最新バージョンは、ここにあります。
そして最後に、春とキュウリの依存関係:
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-spring</artifactId>
<version>6.8.0</version>
<scope>test</scope>
</dependency>
繰り返しになりますが、こちらでCucumberSpringの最新バージョンを確認できます。
3. 構成
次に、CucumberをSpringアプリケーションに統合する方法を見ていきます。
まず、Spring Bootアプリケーションを作成します。これについては、Spring-Bootアプリケーションの記事に従います。 次に、Spring RESTサービスを作成し、そのCucumberテストを記述します。
3.1. RESTコントローラー
まず、簡単なコントローラーを作成しましょう。
@RestController
public class VersionController {
@GetMapping("/version")
public String getVersion() {
return "1.0";
}
}
3.2. きゅうりのステップの定義
JUnitを使用してCucumberテストを実行するために必要なのは、アノテーション @RunWith(Cucumber.class)を使用して単一の空のクラスを作成することだけです。
@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/resources")
public class CucumberIntegrationTest {
}
注釈@CucumberOptionsを見ることができます。ここでは、機能ファイルとも呼ばれるGherkinファイルの場所を指定しています。 この時点で、キュウリはガーキン言語を認識します。 はじめに述べた記事でガーキンについてもっと読むことができます。
それでは、Cucumber機能ファイルを作成しましょう。
Feature: the version can be retrieved
Scenario: client makes call to GET /version
When the client calls /version
Then the client receives status code of 200
And the client receives server version 1.0
シナリオは、RESTサービスのURL / version に対してGET呼び出しを行い、応答を確認することです。
次に、いわゆるグルーコードを作成する必要があります。 これらは、単一のGherkinステップをJavaコードにリンクするメソッドです。
ここでオプションを選択する必要があります。注釈内でキュウリ式または正規表現を使用できます。 この例では、正規表現に固執します。
@When("^the client calls /version$")
public void the_client_issues_GET_version() throws Throwable{
executeGet("http://localhost:8080/version");
}
@Then("^the client receives status code of (\\d+)$")
public void the_client_receives_status_code_of(int statusCode) throws Throwable {
HttpStatus currentStatusCode = latestResponse.getTheResponse().getStatusCode();
assertThat("status code is incorrect : "+
latestResponse.getBody(), currentStatusCode.value(), is(statusCode));
}
@And("^the client receives server version (.+)$")
public void the_client_receives_server_version_body(String version) throws Throwable {
assertThat(latestResponse.getBody(), is(version));
}
それでは、CucumberテストをSpringApplicationContextと統合しましょう。 そのために、新しいクラスを作成し、@SpringBootTestと@CucumberContextConfigurationでアノテーションを付けます。
@CucumberContextConfiguration
@SpringBootTest
public class SpringIntegrationTest {
// executeGet implementation
}
これで、すべてのCucumber定義を、SpringIntegrationTestを拡張する個別のJavaクラスに入れることができます。
public class StepDefs extends SpringIntegrationTest {
@When("^the client calls /version$")
public void the_client_issues_GET_version() throws Throwable {
executeGet("http://localhost:8080/version");
}
}
これで、テスト実行の準備が整いました。
最後に、コマンドラインからクイック実行を実行できます。 mvn clean install -Pintegration-lite-first を実行するだけです。Mavenは統合テストを実行し、結果をコンソールに表示します。
3 Scenarios ([32m3 passed[0m)
9 Steps ([32m9 passed[0m)
0m1.054s
Tests run: 12, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 9.283 sec - in
com.baeldung.CucumberTest
2016-07-30 06:28:20.142 INFO 732 --- [Thread-2] AnnotationConfigEmbeddedWebApplicationContext :
Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext:
startup date [Sat Jul 30 06:28:12 CDT 2016]; root of context hierarchy
Results :
Tests run: 12, Failures: 0, Errors: 0, Skipped: 0
4. 結論
SpringでCucumberを構成したので、BDDテストでSpring構成のコンポーネントを使用すると便利です。 これは、CucumberテストをSpring-Bootアプリケーションに統合するための簡単なガイドです。
いつものように、このチュートリアルに示されているすべてのコードサンプルは、GitHubでから入手できます。