DEV Community

kaede
kaede

Posted on • Updated on

Kotlin Springboot -- Part 11 E2E テスト用に Gauge プロジェクトを作成し、Kotlin でテストを書けるようにする

why

Springboot で作成した API をテストするために
End to End テストを書く。

https://hepokon365.hatenablog.com/entry/2020/11/16/012429

gauge を使うとマークダウンでテストシナリオを書いて、実装を kotlin で作成できるらしい。


Gauge

ゲージで作成すると、Springboot 関係なく、Kotlin のテスト用のアプリケーションが作れるらしい?

https://tech.toreta.in/entry/2020/12/15/110722

Toreta の記事を参考にすると、インストールの仕方とサンプルデータの入れ方が書いてある。

https://docs.gauge.org/getting_started/installing-gauge.html?os=linux&language=csharp&ide=vscode

公式 doc をみると、IntelliJ より VScode 推奨らしい?


インストール

https://docs.gauge.org/getting_started/installing-gauge.html?os=linux&language=java&ide=vscode#installation-instructions-1

curl -SsL https://downloads.gauge.org/stable | sh
Enter fullscreen mode Exit fullscreen mode
 gauge -v
Gauge version: 1.4.3
Commit Hash: f98dd40
Enter fullscreen mode Exit fullscreen mode

curl でインストールできる


コマンドでの実行の仕方

 gauge
Usage:
  gauge <command> [flags] [args]

Examples:
  gauge run specs/
  gauge run --parallel specs/
Enter fullscreen mode Exit fullscreen mode

guage run specs で指定した場所のスペックファイルが実行できるらしい。

実際は mvn test で実行できる。


Gauge の Kotlin プロジェクトを作成

https://yuya-hirooka.hatenablog.com/entry/2021/07/10/220438

yuyua-hirooka さんの記事を参考にする

~/source/gauge-kotlin
 gauge init java_maven
Initializing template from https://github.com/getgauge/template-java-maven/releases/latest/download/java_maven.zip
.
Copying Gauge template java_maven to current directory ...
Successfully initialized the project. Run specifications with "mvn clean test" in project root.
Enter fullscreen mode Exit fullscreen mode

gauge init java_maven で maven_java でプロジェクトを作成した。


作成したプロジェクトを開く

Image description

IntelliJ で開くと、

  • specs/ に Spec ファイルのサンプルの example.spec
  • env/ default/ に設定ファイルのサンプルの default.properties
  • src/test/java/ に Spec の実装のサンプルの StepImplementation.java

これらがセットアップされているのを確認できる。


pom.xml の propeties に Kotlin 1.3.70 を追加

最初は Java しか書けないので、Kotlin も使えるようにする。

    <dependencies>
        <dependency>
            <groupId>com.thoughtworks.gauge</groupId>
            <artifactId>gauge-java</artifactId>
            <version>0.7.10</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.assertj</groupId>
            <artifactId>assertj-core</artifactId>
            <version>3.17.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
Enter fullscreen mode Exit fullscreen mode

pom.xml をみると、セットアップされた

  • gauge-java
  • assertj-core

これらのみが入っている。

https://yuya-hirooka.hatenablog.com/entry/2021/07/10/220438

https://qiita.com/megmogmog1965/items/36f3d778c0397b27b45e#properties

pom.xml の propeties に Kotlin を書いてバージョンを指定する。

    <properties>
        <kotlin.version>1.3.70</kotlin.version>
    </properties>
Enter fullscreen mode Exit fullscreen mode

pom.xml の dependency に kotlin-stdlib と kotlin-test を追加

        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-stdlib</artifactId>
            <version>${kotlin.version}</version>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-test</artifactId>
            <version>${kotlin.version}</version>
            <scope>test</scope>
        </dependency>
Enter fullscreen mode Exit fullscreen mode

dependency に kotlin-stdlib と kotlin-test を追加

Image description

そしてmaven reload すると kotlin のライブラリが入る。


pom.xml の build / pluglins に kotlin-maven-plugin を追加

これがないとビルドできないと推測

plugins に kotlin-maven-plugin を入れて
execution に compile, test-complie
これらの id, phase, goals を書き
test-compile には configuration として sourceDirs の source に src/test/kotlin を指定する

            <plugin>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-maven-plugin</artifactId>
                <version>${kotlin.version}</version>
                <executions>
                    <execution>
                        <id>compile</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>test-compile</id>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>test-compile</goal>
                        </goals>
                        <configuration>
                            <sourceDirs>                 
                             <source>src/test/kotlin</source>
                            </sourceDirs>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
Enter fullscreen mode Exit fullscreen mode

これで kotiln をコンパイルできるようにすると推測


CUI でデフォルトの Spec ファイルを実行する。

GUI は IntelliJ plugin 入れないとできなそうで

クリックで飛ぶのも無理だった。

なので CUI で実行する。

mvn test

# Specification Heading
  ## Vowel counts in single word          
  ## Vowel counts in multiple word        
Enter fullscreen mode Exit fullscreen mode

これで specs/example.spec が動き
CUI で既存の Spec ファイルが実行できた。


Kotlin で Step の実装ファイルを作成して実行する

次は Kotlin で新たに Step ファイルを作成して実行してみる

test / java に並列して test / kotlin を作成

imp.kt という名前で作る

import com.thoughtworks.gauge.Step

class HelloKotlin {
    @Step("test kotlin")
    fun hello(){
        println("Hello, kotlin")
    }
}
Enter fullscreen mode Exit fullscreen mode

完全に Kotlin の文法で Step の実装を書いてみる

Hello Kotlin
---------------------------

* test kotlin
Enter fullscreen mode Exit fullscreen mode

これを mvn test で発動すると

# Specification Heading
  ## Hello Kotlin        Hello, kotlin
 
  ## Vowel counts in multiple word        
Enter fullscreen mode Exit fullscreen mode

読まれた!!!


まとめ

Gauge プロジェクトを作成して Kotlin でテストの実装を書くためには

Gauge をインストールして gauge init でプロジェクトを作成
pom.xml の propeties に Kotlin を追加
pom.xml の dependency に kotlin-stdlib と kotlin-test を追加
pom.xml の build/plugins に kotlin-maven-plugin を追加して
execution に compile, test-complie を追加し
test-complie の sourceDirs の source に src/test/kotlin を追加する

これでプロジェクトの src/test/imp.kotlin を追加して Step を書き
specs/example.spec で Step を呼び出すことで

Kotlin で書いた Step の実装を呼び出せる!


今後

https://tech.uzabase.com/entry/2019/11/12/120539

この記事を参考にして、Before Suit で DB のセットアップも作る

Top comments (0)

An Animated Guide to Node.js Event Loop

Node.js doesn’t stop from running other operations because of Libuv, a C++ library responsible for the event loop and asynchronously handling tasks such as network requests, DNS resolution, file system operations, data encryption, etc.

What happens under the hood when Node.js works on tasks such as database queries? We will explore it by following this piece of code step by step.