対し、今回紹介するBDD(behavior driven development:振舞駆動開発)では、テスト対象の振る舞いに対してのテストを記述します。BDDでは、要求仕様にフォーカスし、よりWhatを意識したテストケースを作成できます。これにより、TDD以上に設計から実装へのトレーサビリティを確保できるようになります。
対象読者
- Java開発者
必要な環境
- JDK 7
- Maven 3
JBehaveとは
JBehaveを利用することにより、JavaでBDDを実践できるようになります。具体的には、storyファイルに記述した要求仕様をベースに検証ロジックを実装し、テストを行います。
事前準備
まずは、Mavenで空のJavaプロジェクトを作成します。以下のようにコマンドプロンプトでmvnコマンドを実行します。
$ mvn archetype:create -DgroupId=com.example -DartifactId=jbehave-example
「BUILD SUCCESS」と表示されれば完了です。次に、プロジェクト直下にある「pom.xml」を以下のように編集します。
<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.example</groupId>
    <artifactId>jbehave-example</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>jbehave-example</name>
    <properties>
        <jbehave.version>3.8</jbehave.version>
        <junit.version>4.11</junit.version>
        <failsafe.and.surefire.version>2.16</failsafe.and.surefire.version>
        
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
        </dependency>
        <dependency>
            <groupId>org.jbehave</groupId>
            <artifactId>jbehave-core</artifactId>
            <version>${jbehave.version}</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${failsafe.and.surefire.version}</version>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>${failsafe.and.surefire.version}</version>
                <executions>
                    <execution>
                        <id>integration-test</id>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <includes>
                        <include>**/*Stories.java</include>
                    </includes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.jbehave</groupId>
                <artifactId>jbehave-maven-plugin</artifactId>
                <version>${jbehave.version}</version>
                <executions>
                    <execution>
                        <id>run-stories-as-embeddables</id>
                        <phase>integration-test</phase>
                        <configuration>
                            <testSourceDirectory>${basedir}/src/test/java</testSourceDirectory>
                            <includes>
                                <include>**/*Stories.java</include>
                            </includes>
                            <ignoreFailureInStories>false</ignoreFailureInStories>
                            <ignoreFailureInView>false</ignoreFailureInView>
                        </configuration>
                        <goals>
                            <goal>run-stories-as-embeddables</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
  JunitとJBehaveのライブラリへの依存を追加し、各種プラグインの設定を行います。jbehave-maven-pluginがmavenからJBehaveを起動するために必要なプラグインであり、さらに、maven-surefire-plugin、maven-failsafe-pluginを利用して実行結果をコンソールに表示します。

 
              
               
                          
                           
                          
                           
                          
                           
                          
                           
                          
                           
                          
                           
                          
                           
                          
                           
                          
                           
                          
                           
                          
                           
                          
                           
                              
                               
                              
                               
                              
                               
                              
                               
                              
                               
                      
                     
                      
                     
                      
                     
                      
                     
                      
                     
                      
                     
                      
                     
															
														 
															
														.png) 
     
     
     
     
     
													 
													 
													 
													 
													 
										
									
 
                    