Slide 1

Slide 1 text

How to Test Server-side Kotlin 2018-09-11 長澤太郎

Slide 2

Slide 2 text

長澤太郎 ● @ngsw_taro ● Ubie株式会社 ソフトウェアエンジニア ● 日本Kotlinユーザグループ代表

Slide 3

Slide 3 text

Redesign Medical Care

Slide 4

Slide 4 text

Kontributer しらじさん JOIN!!!!

Slide 5

Slide 5 text

お疲れ様でした! https://photos.google.com/share/AF1QipMux-IXIIBpf2Rr_HwWtQu5pICYJlVCS9yzDmdVbEm0Gajya5D9-pS0A__jXksrAg?key=N0dDZ1hWd HNFVTFodHlpN1BiaC1laXpqQXJkN1Z3 より引用

Slide 6

Slide 6 text

How to Test Server-side Kotlin エムスリー 前原さん、鈴木さん https://photos.google.com/share/AF1QipMux-IXIIBpf2Rr_HwWtQu5pICYJlVCS9yzDmdVbEm0Gajya5D9-pS0A__jXksrAg?key=N0dDZ1hWd HNFVTFodHlpN1BiaC1laXpqQXJkN1Z3 より引用

Slide 7

Slide 7 text

How to Test Server-side Kotlin 編

Slide 8

Slide 8 text

UbieではKotlin x SpringでAPI開発してます

Slide 9

Slide 9 text

テスト関係のフレームワークやライブラリ ● JUnit5 - Jupiter ● Spring Test - WebTestClient ● AssertJ ● MockK ● DbSetup-kotlin

Slide 10

Slide 10 text

JUnit5 class FooTest { @Nested inner class fooMethod { @Test fun `should throw exception`() { assertThrows() { Foo().foo() } } } }

Slide 11

Slide 11 text

class FooTest { @Nested inner class fooMethod { @Test fun `should throw exception`() { assertThrows() { Foo().foo() } } } } JUnit5

Slide 12

Slide 12 text

JUnit5 class FooTest { @Nested inner class fooMethod { @Test fun `should throw exception`() { assertThrows() { Foo().foo() } } } }

Slide 13

Slide 13 text

AssertJ val got = sut.findUser(id) assertThat(got).isNotNull assertThat(got?.name).isEqualTo("ほげ")

Slide 14

Slide 14 text

AssertJ val got = sut.findUser(id) assertThat(got).isNotNull assertThat(got?.name).isEqualTo("ほげ") Kotlin 1.3 Contractsで実現か!?

Slide 15

Slide 15 text

MockK interface UserRepository { suspend fun findUser(id: Long): User? } val userRepo = mockk() every { userRepo.findUser(1) } returns user

Slide 16

Slide 16 text

MockK interface UserRepository { suspend fun findUser(id: Long): User? } val userRepo = mockk() every { userRepo.findUser(1) } returns user coEvery { userRepo.findUser(1) } returns user

Slide 17

Slide 17 text

WebTestClient - expectBodyメソッド webTestClient.get() .exchange() .expectBody(MyApiBody::class.java) .isEqualTo(expectedBody) NullPointerException が出る!

Slide 18

Slide 18 text

WebTestClient - expectBody拡張関数 webTestClient.get() .exchange() .expectBody() .isEqualTo(expectedBody) import org.springframework.test.web.reactive.server.expectBody いける!

Slide 19

Slide 19 text

WebTestClient - expectBody拡張関数 webTestClient.get() .exchange() .expectBody() .isEqualTo(expectedBody) import org.springframework.test.web.reactive.server.expectBody いける! が、サジェストされない

Slide 20

Slide 20 text

Pluginのバグ(issueあった)

Slide 21

Slide 21 text

しらじさん修正プルリク!!!神!!!!

Slide 22

Slide 22 text

リリースされるのは 1.3.20 乞うご期待