SpringBootTest入門

5.3K Views

May 12, 17

スライド概要

SpringBootでのユニットテストをやったことがない方向けの資料になります。

本資料は、
Yahoo!ショッピング Tech Meeting主催
https://yahoo-shp-tech-meeting.connpass.com
SpringBootハンズオン #1
https://yahoo-shp-tech-meeting.connpass.com/event/53268/
の発表資料になります。

profile-image

2023年10月からSpeaker Deckに移行しました。最新情報はこちらをご覧ください。 https://speakerdeck.com/lycorptech_jp

シェア

またはPlayer版

埋め込む »CMSなどでJSが使えない場合

関連スライド

各ページのテキスト
1.

SpringBootTest入門 2017年5月10日 猪野 凌也 Co p yrig ht © 2 0 1 7 Yaho o Jap an Co rp o ratio n. All Rig hts Reserved .

2.

自己紹介 ・名前 猪野 凌也 (イノ リョウヤ) ・所属 ヤフー株式会社 ショッピングカンパニー (2016新卒) ・業務 ヤフーショッピング商品プラットフォームの開発・運用 ・経験 - PC用2Dゲームエンジンの開発 - インターネット決済システム開発 - 塾運営システム開発 2 Co p yrig ht © 2 0 1 7 Yaho o Jap an Co rp o ratio n. All Rig hts Reserved .

3.

目標 SpringBootアプリケーションで DIコンテナ管理のBeanについての 簡単なテストコードの書き方を理解する ※SprintBootTest入門というタイトルですが、SpringBoot独自のテスト機能ではなく、 Springと共通のテスト機能の入門の説明をSpringBootアプリケーション上で行います ※Webアプリケーション向けのE2Eテストなどについては触れません ※テストフレームワークとしてJUnit4を利用します 3 Co p yrig ht © 2 0 1 7 Yaho o Jap an Co rp o ratio n. All Rig hts Reserved .

4.

目次 1. 2. 3. 4. 5. 6. 4 ユニットテストとは JUnit4の基本 SpringBootTest Beanの単体テスト Beanの結合テスト おまけ Co p yrig ht © 2 0 1 7 Yaho o Jap an Co rp o ratio n. All Rig hts Reserved .

5.

1.ユニットテストとは Co p yrig ht © 2 0 1 7 Yaho o Jap an Co rp o ratio n. All Rig hts Reserved .

6.

ユニットテストとは クラスやメソッドがプログラマの 期待通りに振る舞うかを検証すること テストコードは クラスやメソッドの仕様書ともなりうる 6 Co p yrig ht © 2 0 1 7 Yaho o Jap an Co rp o ratio n. All Rig hts Reserved .

7.

2.Junit4の基本 Co p yrig ht © 2 0 1 7 Yaho o Jap an Co rp o ratio n. All Rig hts Reserved .

8.

JUnit4の基本 SpringBootプロジェクトフォルダ構造 testパッケージ以下に mainパッケージと同じ構造で テストクラスを配置します。 テストパッケージ テストクラス 8 Co p yrig ht © 2 0 1 7 Yaho o Jap an Co rp o ratio n. All Rig hts Reserved .

9.

JUnit4の基本 テストの実行方法の例としては以下の通り [標準コマンド] プロジェクトフォルダで「./mvnw test 」とコマンドを打つ [Eclipse or STS] プロジェクトを右クリックで「Run As」で「JUnitTest」を選択するなど 9 Co p yrig ht © 2 0 1 7 Yaho o Jap an Co rp o ratio n. All Rig hts Reserved .

10.

JUnit4の基本 Eclipseで実行した例 10 Co p yrig ht © 2 0 1 7 Yaho o Jap an Co rp o ratio n. All Rig hts Reserved .

11.

JUnit4の基本 計算値と期待値が一致しているか判定する テストケースメソッドの例 @Test public void test() throws Exception { assertThat(計算値,equalTo(期待値)); } 11 Co p yrig ht © 2 0 1 7 Yaho o Jap an Co rp o ratio n. All Rig hts Reserved .

12.

JUnit4の基本 計算値と期待値が一致しているか判定する テストケースメソッドの例 @Test public void test() throws Exception { assertThat(計算値,equalTo(期待値)); } テストケースのメソッドであることを表す 12 Co p yrig ht © 2 0 1 7 Yaho o Jap an Co rp o ratio n. All Rig hts Reserved .

13.

JUnit4の基本 計算値と期待値が一致しているか判定する テストケースメソッドの例 @Test public void test() throws Exception { assertThat(計算値,equalTo(期待値)); } テストの場合は例外的に基底クラスをthrowsに指定 13 Co p yrig ht © 2 0 1 7 Yaho o Jap an Co rp o ratio n. All Rig hts Reserved .

14.

JUnit4の基本 計算値と期待値が一致しているか判定する テストケースメソッドの例 @Test public void test() throws Exception { assertThat(計算値,equalTo(期待値)); } 値が正しいか判定するAssertメソッド 14 Co p yrig ht © 2 0 1 7 Yaho o Jap an Co rp o ratio n. All Rig hts Reserved .

15.

JUnit4の基本 計算値と期待値が一致しているか判定する テストケースメソッドの例 @Test public void test() throws Exception { assertThat(計算値,equalTo(期待値)); } 一致条件を述語的に示し、Matcherオブジェクトを返す ※ equalToは一例です。 InやinstanceOfなどの便利なMatcherAPIが存在します。 15 Co p yrig ht © 2 0 1 7 Yaho o Jap an Co rp o ratio n. All Rig hts Reserved .

16.

JUnit4の基本 AssertJパッケージを利用したメソッドチェーンによる書き方 @Test public void test() throws Exception { assertThat(計算値).isEqualTo(期待値); } メリット - MatcherAPIを覚えていなくてもIDE補完で楽に記述できる 16 Co p yrig ht © 2 0 1 7 Yaho o Jap an Co rp o ratio n. All Rig hts Reserved .

17.

JUnit4の基本 以下のような表記は、JUnit3で使われていたJUnit標準の機能 @Test public void test() throws Exception { assertEquals(期待値,計算値); } JUnit5では、Matcherオブジェクトを利用するかどうかを、 選択することができるようになり、標準はこちらに戻る方針のようです。 17 Co p yrig ht © 2 0 1 7 Yaho o Jap an Co rp o ratio n. All Rig hts Reserved .

18.

JUnit4の基本 ex) テスト対象クラスの例(Calculator.java) package com.example; public class Calculator { public Integer multiply(Integer a, Integer b) { return a * b; } } 18 Co p yrig ht © 2 0 1 7 Yaho o Jap an Co rp o ratio n. All Rig hts Reserved .

19.

JUnit4の基本 ex) テストクラス(CalculatorTests.java) package com.example; import static org.hamcrest.CoreMatchers.equalTo; import static org.junit.Assert.assertThat; import org.junit.Test; public class CalculatorTests { @Test public void 掛け算のテスト() throws Exception { Calculator calc = new Calculator(); assertThat(calc.multiply(2, 3)).isEqualTo(6); // AssertJ利用 // assertThat(calc.multiply(2, 3), equalTo(6)); // JUnit4標準 } } 19 Co p yrig ht © 2 0 1 7 Yaho o Jap an Co rp o ratio n. All Rig hts Reserved .

20.

3.SpringBootTest Co p yrig ht © 2 0 1 7 Yaho o Jap an Co rp o ratio n. All Rig hts Reserved .

21.

SpringBootTestの準備 SpringInitilizr(https://start.spring.io/) でプロジェクトを作成した場合は、 以下のように自動でpom.xmlに必要なパッケージが 入っているので準備不要。 <dependency> <groupId>org.springframework.boot</groupId > <artifactId>spring-boot-startertest</artifactId> 21 <scope>test</scope> Co p yrig ht 2017 © </dependency> Yaho o Jap an Co rp o ratio n. All Rig hts Reserved .

22.

spring-boot-starter-testについて 「spring-boot-starter-test」には以下のパッケージが含まれます。 22 パッケージ名 概要 JUnit デファクトスタンダードのJavaテストフレームワーク SpringTest & SpringBootTest SpringまたはSpringBootで作られたアプリケーション用テストライブラリ AssertJ Assertionライブラリ Hamcrest matcherオブジェクトライブラリ Mockito Javaのモックフレームワーク JSONassert Json用のAssertionライブラリ JsonPath Json用のXPath Co p yrig ht © 2 0 1 7 Yaho o Jap an Co rp o ratio n. All Rig hts Reserved .

23.

4.Beanの単体テスト Co p yrig ht © 2 0 1 7 Yaho o Jap an Co rp o ratio n. All Rig hts Reserved .

24.

Beanの単体テスト BeanとなっているクラスをDIコンテナを利用 せずにクラス単体でテストする。 1. テスト対象クラスを作成する 2. テストクラスを作成する 24 Co p yrig ht © 2 0 1 7 Yaho o Jap an Co rp o ratio n. All Rig hts Reserved .

25.

Beanの単体テスト テスト対象クラス(NameGreetingService.java) @Component public class NameGreetingService { // 名前を呼んで挨拶することのできるサービスクラス @Value("Tom") public String name; @Value("Hello") public String message; public String greet() { return message + "," + name; } } 25 Co p yrig ht © 2 0 1 7 Yaho o Jap an Co rp o ratio n. All Rig hts Reserved .

26.

Beanの単体テスト テストクラス(NameGreetingServiceTests.java) public class NameGreetingServiceTests { @Test public void 名前付き挨拶テスト() throws Exception { NameGreetingService service = new NameGreetingService(); String expected = service.message + "," + service.name; assertThat(service.greet()).isEqualTo(expected); } } ※1 テストクラスはtestパッケージ内に配置すること ※2 テストクラスの名前は基本的には「(クラス名)Tests」が通例 ※3 テストケースメソッド名は可能ならば日本語の方がわかりやすい 26 Co p yrig ht © 2 0 1 7 Yaho o Jap an Co rp o ratio n. All Rig hts Reserved .

27.

Mockを用いたBeanの単体テスト ここで、疑問が! 27 Co p yrig ht © 2 0 1 7 Yaho o Jap an Co rp o ratio n. All Rig hts Reserved .

28.

Mockを用いたBeanの単体テスト newでインスタンス生成を行うと、DIコンテナを利用していないか ら、Beanが依存しているクラスのDIが行われないよね! クラスのプロパティにDIが必要な場合はどうするの? @Component public class DINameGreetingService { @Autowired NameGreetingSource source; public String greet() { return source.message + "," + source.name; } } 28 Co p yrig ht © 2 0 1 7 Yaho o Jap an Co rp o ratio n. All Rig hts Reserved .

29.

Mockを用いたBeanの単体テスト このような場合にどうするか… @Component @Component public class NameGreetingSource { public class DINameGreetingService { @Value("Tom") @Autowired public String name; NameGreetingSource source; DI @Value("Hello") public String greet() { public String message; return source.message + "," + source.name; } } } 29 Co p yrig ht © 2 0 1 7 Yaho o Jap an Co rp o ratio n. All Rig hts Reserved .

30.

Mockを用いたBeanの単体テスト DIコンテナは使わずに Mockクラスを使ってDIしよう! 30 Co p yrig ht © 2 0 1 7 Yaho o Jap an Co rp o ratio n. All Rig hts Reserved .

31.

Mockを用いたBeanの単体テスト 登場!!! 画像:http://site.mockito.orgより 31 Co p yrig ht © 2 0 1 7 Yaho o Jap an Co rp o ratio n. All Rig hts Reserved .

32.

Mockを用いたBeanの単体テスト @RunWith(MockitoJUnitRunner.class) // ①テストランナーにMockitを指定 public class DINameGreetingServiceTests { @InjectMocks // ②InjectされるMockオブジェクト DINameGreetingService service; @Mock // ③InjectするMockオブジェクト NameGreetingSource source; @Test public void Mockを利用した名前付き挨拶テスト() throws Exception { String expected = source.message + "," + source.name; assertThat(service.greet()).isEqualTo(expected); } } ※追記:SpringBoot1.4以降であれば、@MockBeanを使うと、より楽にMockオブジェクトを生成できます 32 Co p yrig ht © 2 0 1 7 Yaho o Jap an Co rp o ratio n. All Rig hts Reserved .

33.

Mockを用いたBeanの単体テスト② MockitoでMockクラスとしたオブジェクトは、 メソッドを実際に実行せずに、メソッドの振る舞いを引数に よって指定することができたりします。 Mockitチュートリアル http://maman-it-information.readthedocs.io/ja/latest/src/junit/mockito.html 33 Co p yrig ht © 2 0 1 7 Yaho o Jap an Co rp o ratio n. All Rig hts Reserved .

34.

5.Beanの結合テスト Co p yrig ht © 2 0 1 7 Yaho o Jap an Co rp o ratio n. All Rig hts Reserved .

35.

Beanの結合テスト SpringBootのDIコンテナを利用して 結合テストを行いたい場合 35 Co p yrig ht © 2 0 1 7 Yaho o Jap an Co rp o ratio n. All Rig hts Reserved .

36.

Beanの結合テスト BeanのConfigrationクラスを用意して テストクラスのConfigurationに 指定する必要がある 36 Co p yrig ht © 2 0 1 7 Yaho o Jap an Co rp o ratio n. All Rig hts Reserved .

37.

Beanの結合テスト @Configuration // Configurationクラスであることを指定 @ComponentScan(“com.example”) // ComponentScan対象パッケージ指定 public class AppConfig { @Bean // テスト用のBeanの生成を登録 public NameGreetingSource nameGreetingSource() { NameGreetingSource source = new NameGreetingSource(); source.name = "Test"; source.message = "Good morning!"; return source; } } 37 Co p yrig ht © 2 0 1 7 Yaho o Jap an Co rp o ratio n. All Rig hts Reserved .

38.

Beanの結合テスト @RunWith(SpringJUnit4ClassRunner.class) // DIコンテナを使うために指定 @ContextConfiguration(classes = AppConfig.class) // Configクラスを指定 public class ConfigDINameGreetingServiceTests { @Autowired // テストクラスにDIする DINameGreetingService service; @Test public void Bean設定を用いたDIコンテナを利用した名前付き挨拶テスト() { String expected = service.source.message + "," + service.source.name; assertThat(service.greet()).isEqualTo(expected); } } 38 Co p yrig ht © 2 0 1 7 Yaho o Jap an Co rp o ratio n. All Rig hts Reserved .

39.

まとめ 1. Beanの単体テスト(DI無) -> 基本のJUnitのテスト 2. Beanの単体テスト(DI有) -> mockitoを用いたテスト 3. Beanの結合テスト(DIコンテナ有) -> テスト用のDIコンテナ設定を読み込んでテスト 39 Co p yrig ht © 2 0 1 7 Yaho o Jap an Co rp o ratio n. All Rig hts Reserved .

40.

6.おまけ Co p yrig ht © 2 0 1 7 Yaho o Jap an Co rp o ratio n. All Rig hts Reserved .

41.

便利なテスト 今回は入門編なので説明しませんでしたが、 他にも様々な便利なテストができます! - MockMVCパッケージによるControllerクラスのテスト - SpringBootTestによる、組み込みWebサーバーでの結合テスト - HtmlUnitパッケージにより、Html操作でのEnd to Endテスト などなど… 41 Co p yrig ht © 2 0 1 7 Yaho o Jap an Co rp o ratio n. All Rig hts Reserved .

42.

参考文献 42 Co p yrig ht © 2 0 1 7 Yaho o Jap an Co rp o ratio n. All Rig hts Reserved .

43.

EOP Co p yrig ht © 2 0 1 7 Yaho o Jap an Co rp o ratio n. All Rig hts Reserved .