Upgrade to Pro — share decks privately, control downloads, hide ads and more …

テストから始めるKotlin導入

97370d6641bedd9849c3974c65935c0d?s=47 futabooo
July 12, 2017

 テストから始めるKotlin導入

97370d6641bedd9849c3974c65935c0d?s=128

futabooo

July 12, 2017
Tweet

Transcript

  1. Start Kotlin with Test ςετ͔Β࢝ΊΔKotlinಋೖ

  2. 2 About me  4FOJPS&OHJOFFS4DSVN.BTUFSBUFVSFLB *OD  +BWB ,PUMJO (PMBOH

    5ZQF4DSJQU "OHVMBS+4  'BOUBTZ&BSUI;FSP T$3:FE  GVUBCPPP ɹɹɹೋ઒ ོߒʢTakahiro Futagawaʣ
  3. 3 About eureka  ैۀһ਺  ໊ʢฏۉ೥ྸࡀʣ  ࣄۀ಺༰ 

    ࣗࣾαʔϏεاըɾ։ൃɾӡӦ  1BJST  $PVQMFT ձࣾ֓ཁ
  4. 'BDFCPPLΛར༻ͨ͠ ࿀Ѫɾࠗ׆ϚονϯάαʔϏε ྦྷܭձһ਺ 500ສ ਓ ಥഁ͠·ͨ͠

  5. ΧοϓϧͷͨΊͷΞϓϦ ࠃ಺࠷େڃ 400ສ μ΢ϯϩʔυ

  6. 6 Pairs  +BWB ,PUMJO  %BHHFS  0SNB 

    0L)UUQ 3FUSPpU  #VUUFS,OJGF %BUB#JOEJOH  3Y+BWB 3Y,PUMJO FUD Android JP
  7. 7 Couples  +BWB  "DUJWF"OESPJE  7PMMFZ 0L)UUQ 3FUSPpU

    4PDLFU*0  3Y+BWB  FUD Android
  8. Pairs Androidͷྺ࢙ 8

  9. 9 େੲͷPairsAndroid

  10. 10 ݱࡏͷPairsAndroid

  11. 11 ࠷ۙϦχϡʔΞϧͨ͠Global൛

  12. 12 –R. Kulawiec “े෼ʹൃୡͨ͠όά͸ͲΕ΋ɺػೳͱݟ෼͚͕͔ͭͳ͍”

  13. None
  14. ʊਓਓਓਓਓਓਓਓਓਓਓਓਓਓʊ ʼɹॏཁͳػೳ͕࢖͑ͳ͍όάɹʻ ʉ̮̮̮̮̮̮̮̮̮̮̮̮̮̮ʉ

  15. ςετ͕ॻ͍ͯ͋Ε͹๷͛ͨ

  16. ςετ͕ॻ͚Δঢ়ଶͰ͸ͳ͔ͬͨ

  17. ςετ͕ॻ͖΍͍͢ߏ੒ʹมߋ͢Δ 17

  18. 18 ͍͔ͭͷPairsAndroid

  19. 19 Before &OUJUZ%BP %BUBCBTF "1* $MJFOU "DUJWJUZ'SBHNFOU

  20. 20 After Dao %BUBCBTF API Client Repository UseCase Presenter Activity

    / Fragment Service
  21. 21 ৄࡉ https://speakerdeck.com/lvla/activitywogai-shan-sitahua

  22. Kotlinಋೖ 22

  23. 23 AndroidStudio Preferences > Plugins > Browse Repositories

  24. 24 build.gradle buildscript { dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.1.3-2" } }

    allprojects { repositories { maven { url 'http://dl.bintray.com/ngsw-taro/maven' } maven { url 'https://jitpack.io' } } }
  25. 25 app/build.gradle apply plugin: 'kotlin-android' dependencies { testCompile 'junit:junit:4.12' testCompile

    'org.jetbrains.kotlin:kotlin-stdlib:1.1.3-2' testCompile 'com.taroid.knit:knit:0.1.2' testCompile 'com.github.sys1yagi:kmockito:0.1.2' }
  26. KotlinͰςετΛॻ͍͍ͯ͘ 26

  27. 27 Client Dao %BUBCBTF API Client Repository UseCase Presenter Activity

    / Fragment Service  3FUSPpUͷ*OUFSGBDFΛར༻ͯ͠"1*௨৴ Λߦ͏  "1*ͷϨεϙϯεΛΞϓϦ಺Ͱར༻͠΍ ͍͢ܗʹม׵͢Δ
  28. 28 Client class UserClient (private val service: UserService) {
 


    fun fetchSearchUsers(): Observable<List<User>> {
 return service.fetchSearchUsers()
 .map { response -> UserConverter.convert(response) }
 }
 }
  29. 29 Client val userService = mock<UserService>()
 val client = UserClient(userService)


    
 @Test fun testFetchSearchUsers() {
 val expected = Array(10) { MockUser.newInstance(it) }.toList()
 userService.getUsers().invoked.thenReturn(expected.toObservable()) 
 client.fetchUsers().test().run {
 assertNoErrors()
 assertValue(expected)
 assertCompleted()
 }
 }
  30. 30 Client val userService = mock<UserService>()
 val client = UserClient(userService)


    
 @Test fun testFetchSearchUsers() {
 val expected = Array(10) { MockUser.newInstance(it) }.toList()
 userService.getUsers().invoked.thenReturn(expected.toObservable()) 
 client.fetchUsers().test().run {
 assertNoErrors()
 assertValue(expected)
 assertCompleted()
 }
 }
  31. 31 Client val userService = mock<UserService>()
 val client = UserClient(userService)


    
 @Test fun testFetchSearchUsers() {
 val expected = Array(10) { MockUser.newInstance(it) }.toList()
 userService.getUsers().invoked.thenReturn(expected.toObservable()) 
 client.fetchUsers().test().run {
 assertNoErrors()
 assertValue(expected)
 assertCompleted()
 }
 }
  32. 32 Client val userService = mock<UserService>()
 val client = UserClient(userService)


    
 @Test fun testFetchSearchUsers() {
 val expected = Array(10) { MockUser.newInstance(it) }.toList()
 userService.getUsers().invoked.thenReturn(expected.toObservable()) 
 client.fetchUsers().test().run {
 assertNoErrors()
 assertValue(expected)
 assertCompleted()
 }
 }
  33. 33 Client val userService = mock<UserService>()
 val client = UserClient(userService)


    
 @Test fun testFetchSearchUsers() {
 val expected = Array(10) { MockUser.newInstance(it) }.toList()
 userService.getUsers().invoked.thenReturn(expected.toObservable()) 
 client.fetchUsers().test().run {
 assertNoErrors()
 assertValue(expected)
 assertCompleted()
 }
 }
  34. 34 Client val userService = mock<UserService>()
 val client = UserClient(userService)


    
 @Test fun testFetchSearchUsers() {
 val expected = Array(10) { MockUser.newInstance(it) }.toList()
 userService.getUsers().invoked.thenReturn(expected.toObservable()) 
 client.fetchUsers().test().run {
 assertNoErrors()
 assertValue(expected)
 assertCompleted()
 }
 }
  35. 35 Client val userService = mock<UserService>()
 val client = UserClient(userService)


    
 @Test fun testFetchSearchUsers() {
 val expected = Array(10) { MockUser.newInstance(it) }.toList()
 userService.getUsers().invoked.thenReturn(expected.toObservable()) 
 client.fetchUsers().test().run {
 assertNoErrors()
 assertValue(expected)
 assertCompleted()
 }
 }
  36. 36 Dao Dao %BUBCBTF API Client Repository UseCase Presenter Activity

    / Fragment Service  %BUBCBTFͷ$36%ॲཧΛߦ͏
  37. 37 Dao class UserDao (private val orma: OrmaDatabase) {
 fun

    save(users: List<User>) {
 orma.transactionSync {
 orma.prepareInsertIntoUser(OnConflict.REPLACE)
 .executeAll(users)
 }
 }
 
 fun findAll(): Observable<List<User>> {
 return orma.selectFromUser()
 .executeAsObservable().toList()
 }
 }
  38. 38 Dao val orma = OrmaDatabase.builder(context).name(null).build() val dao = UserDao(orma)

    @Test fun testSave() {
 val dummyUsers = Array(10) { MockUser.newInstance(it) }
 dao.transactionSync { dummyUsers.forEach { dao.save(it) } }
 
 dummyUsers.forEach {
 val found = orma.selectFromUser().userRandIdEq(it.userId).get(0)
 found.rshould be it
 }
 }
  39. 39 Dao val orma = OrmaDatabase.builder(context).name(null).build() val dao = UserDao(orma)

    @Test fun testSave() {
 val dummyUsers = Array(10) { MockUser.newInstance(it) }
 dao.transactionSync { dummyUsers.forEach { dao.save(it) } }
 
 dummyUsers.forEach {
 val found = orma.selectFromUser().userRandIdEq(it.userId).get(0)
 found.rshould be it
 }
 }
  40. 40 Dao val orma = OrmaDatabase.builder(context).name(null).build() val dao = UserDao(orma)

    @Test fun testSave() {
 val dummyUsers = Array(10) { MockUser.newInstance(it) }
 dao.transactionSync { dummyUsers.forEach { dao.save(it) } }
 
 dummyUsers.forEach {
 val found = orma.selectFromUser().userRandIdEq(it.userId).get(0)
 found.rshould be it
 }
 }
  41. 41 Dao val orma = OrmaDatabase.builder(context).name(null).build() val dao = UserDao(orma)

    @Test fun testSave() {
 val dummyUsers = Array(10) { MockUser.newInstance(it) }
 dao.transactionSync { dummyUsers.forEach { dao.save(it) } }
 
 dummyUsers.forEach {
 val found = orma.selectFromUser().userRandIdEq(it.userId).get(0)
 found.rshould be it
 }
 }
  42. 42 Dao val orma = OrmaDatabase.builder(context).name(null).build() val dao = UserDao(orma)

    @Test fun testSave() {
 val dummyUsers = Array(10) { MockUser.newInstance(it) }
 dao.transactionSync { dummyUsers.forEach { dao.save(it) } }
 
 dummyUsers.forEach {
 val found = orma.selectFromUser().userRandIdEq(it.userId).get(0)
 found.rshould be it
 }
 }
  43. 43 Dao val orma = OrmaDatabase.builder(context).name(null).build() val dao = UserDao(orma)

    @Test fun testSave() {
 val dummyUsers = Array(10) { MockUser.newInstance(it) }
 dao.transactionSync { dummyUsers.forEach { dao.save(it) } }
 
 dummyUsers.forEach {
 val found = orma.selectFromUser().userRandIdEq(it.userId).get(0)
 found.rshould be it
 }
 }
  44. 44 Repository Dao %BUBCBTF API Client Repository UseCase Presenter Activity

    / Fragment Service  σʔλͷऔಘɾอଘΛߦ͏
  45. 45 Repository class UserRepository ( private val dao: UserDao, private

    val client: UserClient) { 
 fun searchUsers(): Observable<List<User>> {
 return client.searchUsers() .doOnNext { users -> dao.save(users) } .onErrorResumeNext { dao.findAll() }
 }
 }
  46. 46 Repository val dao = mock<UserDao>(); val client = mock<UserClient>()

    val repository = UserRepository(dao, client) 
 fun testSearchUsers() { val expected = Array(10) { MockUser.newInstance(it) }.toList() client.fetchUsers().invoked.thenReturn(expected.toObservable()) val subscriber = TestSubscriver<User>() repository.searchUsers().subscribe(subscriber) with(subscriber){ assertNoErrors() onNextEvents.size.should be 1 assertCompleted() } }
  47. 47 Repository val dao = mock<UserDao>(); val client = mock<UserClient>()

    val repository = UserRepository(dao, client) 
 fun testSearchUsers() { val expected = Array(10) { MockUser.newInstance(it) }.toList() client.fetchUsers().invoked.thenReturn(expected.toObservable()) val subscriber = TestSubscriver<User>() repository.searchUsers().subscribe(subscriber) with(subscriber){ assertNoErrors() onNextEvents.size.should be 1 assertCompleted() } }
  48. 48 Repository val dao = mock<UserDao>(); val client = mock<UserClient>()

    val repository = UserRepository(dao, client) 
 fun testSearchUsers() { val expected = Array(10) { MockUser.newInstance(it) }.toList() client.fetchUsers().invoked.thenReturn(expected.toObservable()) val subscriber = TestSubscriver<User>() repository.searchUsers().subscribe(subscriber) with(subscriber){ assertNoErrors() onNextEvents.size.should be 1 assertCompleted() } }
  49. 49 Repository val dao = mock<UserDao>(); val client = mock<UserClient>()

    val repository = UserRepository(dao, client) 
 fun testSearchUsers() { val expected = Array(10) { MockUser.newInstance(it) }.toList() client.fetchUsers().invoked.thenReturn(expected.toObservable()) val subscriber = TestSubscriver<User>() repository.searchUsers().subscribe(subscriber) with(subscriber){ assertNoErrors() onNextEvents.size.should be 1 assertCompleted() } }
  50. 50 Repository val dao = mock<UserDao>(); val client = mock<UserClient>()

    val repository = UserRepository(dao, client) 
 fun testSearchUsers() { val expected = Array(10) { MockUser.newInstance(it) }.toList() client.fetchUsers().invoked.thenReturn(expected.toObservable()) val subscriber = TestSubscriver<User>() repository.searchUsers().subscribe(subscriber) with(subscriber){ assertNoErrors() onNextEvents.size.should be 1 assertCompleted() } }
  51. 51 Repository val dao = mock<UserDao>(); val client = mock<UserClient>()

    val repository = UserRepository(dao, client) 
 fun testSearchUsers() { val expected = Array(10) { MockUser.newInstance(it) }.toList() client.fetchUsers().invoked.thenReturn(expected.toObservable()) val subscriber = TestSubscriver<User>() repository.searchUsers().subscribe(subscriber) with(subscriber){ assertNoErrors() onNextEvents.size.should be 1 assertCompleted() } }
  52. 52 Repository val dao = mock<UserDao>(); val client = mock<UserClient>()

    val repository = UserRepository(dao, client) 
 fun testSearchUsers() { val expected = Array(10) { MockUser.newInstance(it) }.toList() client.fetchUsers().invoked.thenReturn(expected.toObservable()) val subscriber = TestSubscriver<User>() repository.searchUsers().subscribe(subscriber) with(subscriber){ assertNoErrors() onNextEvents.size.should be 1 assertCompleted() } }
  53. 53 Repository val dao = mock<UserDao>(); val client = mock<UserClient>()

    val repository = UserRepository(dao, client) 
 fun testSearchUsers() { val expected = Array(10) { MockUser.newInstance(it) }.toList() client.fetchUsers().invoked.thenReturn(expected.toObservable()) val subscriber = TestSubscriver<User>() repository.searchUsers().subscribe(subscriber) with(subscriber){ assertNoErrors() onNextEvents.size.should be 1 assertCompleted() } }
  54. 54 UseCase Dao %BUBCBTF API Client Repository UseCase Presenter Activity

    / Fragment Service  ϏδωεϩδοΫΛදݱ͢Δ
  55. 55 UseCase class UserUseCase (private val repository: UserRepository) { public

    Observable<List<User>> searchUsers() {
 return repository.searchUsers();
 } public Observable<List<User>> fetchRecommendLikeUsers() {
 return repository.fetchRecommendLikeUsers();
 } }
  56. 56 UseCase val repository = mock<UserRepositry> val useCase = UserUseCase(repository)

    @Test fun fetchStatus() {
 val expected = Array(10) { MockUser.newInstance(it) }.toList()
 repository.fetchUsers().invoked.thenReturn(expected.toObservable())
 
 useCase.searchUsers().test().run {
 assertNoErrors()
 assertValues(expected)
 assertCompleted()
 }
 }
  57. 57 UseCase val repository = mock<UserRepositry> val useCase = UserUseCase(repository)

    @Test fun fetchStatus() {
 val expected = Array(10) { MockUser.newInstance(it) }.toList()
 repository.fetchUsers().invoked.thenReturn(expected.toObservable())
 
 useCase.searchUsers().test().run {
 assertNoErrors()
 assertValues(expected)
 assertCompleted()
 }
 }
  58. 58 UseCase val repository = mock<UserRepositry> val useCase = UserUseCase(repository)

    @Test fun fetchStatus() {
 val expected = Array(10) { MockUser.newInstance(it) }.toList()
 repository.fetchUsers().invoked.thenReturn(expected.toObservable())
 
 useCase.searchUsers().test().run {
 assertNoErrors()
 assertValues(expected)
 assertCompleted()
 }
 }
  59. 59 UseCase val repository = mock<UserRepositry> val useCase = UserUseCase(repository)

    @Test fun fetchStatus() {
 val expected = Array(10) { MockUser.newInstance(it) }.toList()
 repository.fetchUsers().invoked.thenReturn(expected.toObservable())
 
 useCase.searchUsers().test().run {
 assertNoErrors()
 assertValues(expected)
 assertCompleted()
 }
 }
  60. 60 UseCase val repository = mock<UserRepositry> val useCase = UserUseCase(repository)

    @Test fun fetchStatus() {
 val expected = Array(10) { MockUser.newInstance(it) }.toList()
 repository.fetchUsers().invoked.thenReturn(expected.toObservable())
 
 useCase.searchUsers().test().run {
 assertNoErrors()
 assertValues(expected)
 assertCompleted()
 }
 }
  61. 61 UseCase val repository = mock<UserRepositry> val useCase = UserUseCase(repository)

    @Test fun fetchStatus() {
 val expected = Array(10) { MockUser.newInstance(it) }.toList()
 repository.fetchUsers().invoked.thenReturn(expected.toObservable())
 
 useCase.searchUsers().test().run {
 assertNoErrors()
 assertValues(expected)
 assertCompleted()
 }
 }
  62. ·ͱΊ 62

  63. 63 ·ͱΊ  Τ΢ϨΧ͸ώτΛͭͳ͙αʔϏεΛఏڙ͍ͯ͠Δ  ςετ͕ແ͍ঢ়ଶ͸ࣄނ͕ى͜Γ΍͍͢ঢ়ଶͰ͋Δ  ,PUMJOಋೖͷલʹมߋɾ௥Ճʹڧ͍ίʔυϕʔεͷํ͕ΠϯύΫτେ͖͍  ϓϩμΫγϣϯίʔυʹ,PUMJOΛೖΕΔͷ͕ා͍࣌͸ςετίʔυ͔Βೖ

    ΕͯΈΔͱΑ͍
  64. Credit: NASA Earth Observatory/NOAA NGDC Thank you :)