1.はじめに
この短いチュートリアルでは、https://www.baeldung.com/spring-boot-start[Spring Boot]のhttps://www.baeldung.com/spring-のサポート力を組み合わせることの利点を示します。それが単体であるかhttps://www.baeldung.com/integration-testing-inであるかに関わらず、boot-testing[テストフレームワーク]とhttps://www.baeldung.com/groovy-spock[Spockフレームワーク]の表現-spring[統合テスト]
2.プロジェクトの設定
簡単なWebアプリケーションから始めましょう。あいさつをしたり、あいさつを変更したり、単純なREST呼び出しでデフォルトにリセットしたりできます。メインクラスとは別に、機能を提供するために単純な RestController を使用します。
@RestController
@RequestMapping("/hello")
public class WebController {
@GetMapping
public String salutation() {
return "Hello world!";
}
}
そのため、コントローラーは「Hello world!」と挨拶します。 @RestController アノテーションとhttps://www.baeldung.com/spring-new-requestmapping-shortcuts[ショートカットアノテーション]のアノテーションでRESTエンドポイント登録
3. SpockとSpring Boot TestのMaven ** 依存関係+
**
まずMavenの依存関係を追加し、必要に応じてMavenプラグインの設定を行います。
3.1. Spring SupportでSpockフレームワークの依存関係を追加する
Spock自体 https://search.maven.org/classic/#search%7Cga%7C1%7C%20(g%3A%22org.spockframework%22%20AND%20a%3A%22spock-spring%22)[Spring用2つの依存関係が必要です。
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<version>1.2-groovy-2.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-spring</artifactId>
<version>1.2-groovy-2.4</version>
<scope>test</scope>
</dependency>
で指定されたバージョンは、使用されているgroovyバージョンへの参照です。
3.2. Spring Bootテストを追加する
Spring Boot Test 、次の依存関係が必要です。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.1.1.RELEASE</version>
<scope>test</scope>
</dependency>
3.3. Groovyを設定する
そしてSpockはhttps://www.baeldung.com/groovy-language[Groovy]に基づいているので、 私たちのテストでこの言語を使うことができるように gmavenplus プラグインを追加して設定する必要があります。
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<goals>
<goal>compileTests</goal>
</goals>
</execution>
</executions>
</plugin>
Groovyはテスト目的でのみ必要なので、プラグインの目標を compileTest に制限していることに注意してください。
4. Spockテストで ApplicationContext を読み込む
簡単なテストの1つは、Springアプリケーションコンテキスト内のすべてのBeansが作成されたかどうかを確認することです。
@SpringBootTest
class LoadContextTest extends Specification {
@Autowired (required = false)
private WebController webController
def "when context is loaded then all expected beans are created"() {
expect: "the WebController is created"
webController
}
}
この統合テストでは、 ApplicationContext を起動する必要があります。これが @ SpringBootTest の処理です。 Spockは、テストでセクションの区切りを“ when” 、“ then” 、または“ expect” のようなキーワードで提供します。
また、https://www.baeldung.com/groovy-language[Groovy Truth]を利用して、Beanがnullかどうかを確認することもできます。これは、このテストの最後の行です。
5.スポックテストで WebMvcTest を使う
同様に、 WebController の動作をテストできます。
@AutoConfigureMockMvc
@WebMvcTest
class WebControllerTest extends Specification {
@Autowired
private MockMvc mvc
def "when get is performed then the response has status 200 and content is 'Hello world!'"() {
expect: "Status is 200 and the response is 'Hello world!'"
mvc.perform(get("/hello"))
.andExpect(status().isOk())
.andReturn()
.response
.contentAsString == "Hello world!"
}
}
重要なのは、私たちのSpockテスト(あるいは _仕様) _ 私たちが使っているhttps://www.baeldung.com/spring-boot-testing[Spring Boot test framework]からのよく知られた注釈をすべて使うことができるということですに。
6. 結論
この記事では、SpockとSpring Bootテストフレームワークを組み合わせて使用するようにMavenプロジェクトを設定する方法について説明しました。さらに、私たちは両方のフレームワークがお互いをどのように補完するかを見てきました。
もっと深く掘り下げるには、https://www.baeldung.com/spring-boot-testing[Spring Bootによるテスト]についての私達のチュートリアル、https://www.baeldung.com/groovy-spockについてを見てください[Spockフレームワーク]およびhttps://www.baeldung.com/groovy-language[Groovy言語]について。
最後に、追加の例を含むソースコードは、https://github.com/eugenp/tutorials/tree/master/spring-boot-testing[GitHub repository]にあります。