キュウリの春の統合
1概要
キュウリはRubyプログラミング言語で書かれた非常に強力なテストフレームワークで、BDD(ビヘイビアドリブン開発)方法論に従います。その目的は、開発者が非技術的な利害関係者によって検証可能なプレーンテキストでハイレベルユースケースを記述し、それらを実行可能テストに変換することを可能にすることです。
/cucumber-rest-api-testing[別の記事]で、これらについてはすでにリンクで説明しました。
そしてhttps://spring.io/blog/2013/08/04/webinar-replay-spring-with-cucumber-for-automation[Cucumber-Spring Integration]はテストの自動化を容易にするためのものです。 CucumberテストをSpringと統合したら、Mavenビルドと共にそれらを実行できるはずです。
2 Mavenの依存関係
Maven依存関係を定義することによってCucumber-Spring統合の使用を始めましょう – Cucumber-JVM依存関係から始めましょう:
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.2.4</version>
<scope>test</scope>
</dependency>
Cucumber JVMの最新版はhttps://mvnrepository.com/artifact/info.cukes/cucumber-jvm[ここ]にあります。
次に、JUnitとCucumberのテスト依存関係を追加します。
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.2.4</version>
<scope>test</scope>
</dependency>
Cucumber JUnitの最新版はhttps://mvnrepository.com/artifact/info.cukes/cucumber-junit[ここ]にあります。
そして最後に、SpringとCucumberの依存関係:
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-spring</artifactId>
<version>1.2.4</version>
<scope>test</scope>
</dependency>
Cucumber Springの最新版はhttps://mvnrepository.com/artifact/info.cukes/cucumber-spring[ここ]にあります。
3構成
それでは、CucumberをSpring Microのサービスアプリケーションに統合する方法について見ていきましょう。最初のステップはSpring Bootアプリケーションを作成することです – そのために私達はリンクをたどります:/spring-boot-application-configuration[Spring-bootアプリケーションの記事]。
次に、BootアプリケーションでSpring RESTサービスを作成し、このRESTサービス用のCucumberテストを作成します。
3.1. RESTコントローラー
最初のステップとして、単純なREST API用のコントローラークラスを作成しましょう。
@RestController
public class VersionController {
@RequestMapping(method={RequestMethod.GET},value={"/version"})
public String getVersion() {
return "1.0";
}
}
3.2. キュウリのステップの定義
JUnitランナーはJUnitフレームワークを使ってCucumber Testを実行します。必要なのは、
@ RunWith(Cucumber.class)
というアノテーションを付けた単一の空のクラスを作成することだけです。
@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/resources")
public class CucumberTest {
}
アノテーション
@ CucumberOptions
が表示されています。ここで、Gherkinファイルの場所を指定しています。この時点で、CucumberはGherkin言語を認識しています。 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呼び出しを行い、応答をアサートすることです。
次のステップは、このテストケースに対応するように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));
}
だから今、私たちはMavenとSpringを使ってこれらのCucumberテストケースメソッドを実行する必要があります。 Mavenがこれらのテストクラスを実行できるように、Spring-JUnitで実行できるクラスを作成します。
@ContextConfiguration(
classes = SpringDemoApplication.class,
loader = SpringApplicationContextLoader.class)
@WebAppConfiguration
@IntegrationTest
public class SpringIntegrationTest {
}
これで、すべてのCucumber定義は、上記のJavaクラスに拡張された個別の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 ** – 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設定のコンポーネントを使うのは便利でしょう。これは、Spring-BootアプリケーションにCucumberテストを統合するための簡単なガイドです。
linked GitHub repository
には、この記事のコードに基づいたサンプルプロジェクトがあります。