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

テストから始めるKotlin導入

futabooo
July 12, 2017

 テストから始めるKotlin導入

futabooo

July 12, 2017
Tweet

More Decks by futabooo

Other Decks in Technology

Transcript

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

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

    ࣗࣾαʔϏεاըɾ։ൃɾӡӦ  1BJST  $PVQMFT ձࣾ֓ཁ
  3. 6 Pairs  +BWB ,PUMJO  %BHHFS  0SNB 

    0L)UUQ 3FUSPpU  #VUUFS,OJGF %BUB#JOEJOH  3Y+BWB 3Y,PUMJO FUD Android JP
  4. 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' } } }
  5. 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' }
  6. 27 Client Dao %BUBCBTF API Client Repository UseCase Presenter Activity

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


    fun fetchSearchUsers(): Observable<List<User>> {
 return service.fetchSearchUsers()
 .map { response -> UserConverter.convert(response) }
 }
 }
  8. 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()
 }
 }
  9. 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()
 }
 }
  10. 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()
 }
 }
  11. 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()
 }
 }
  12. 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()
 }
 }
  13. 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()
 }
 }
  14. 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()
 }
 }
  15. 36 Dao Dao %BUBCBTF API Client Repository UseCase Presenter Activity

    / Fragment Service  %BUBCBTFͷ$36%ॲཧΛߦ͏
  16. 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()
 }
 }
  17. 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
 }
 }
  18. 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
 }
 }
  19. 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
 }
 }
  20. 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
 }
 }
  21. 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
 }
 }
  22. 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
 }
 }
  23. 44 Repository Dao %BUBCBTF API Client Repository UseCase Presenter Activity

    / Fragment Service  σʔλͷऔಘɾอଘΛߦ͏
  24. 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() }
 }
 }
  25. 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() } }
  26. 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() } }
  27. 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() } }
  28. 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() } }
  29. 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() } }
  30. 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() } }
  31. 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() } }
  32. 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() } }
  33. 54 UseCase Dao %BUBCBTF API Client Repository UseCase Presenter Activity

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

    Observable<List<User>> searchUsers() {
 return repository.searchUsers();
 } public Observable<List<User>> fetchRecommendLikeUsers() {
 return repository.fetchRecommendLikeUsers();
 } }
  35. 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()
 }
 }
  36. 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()
 }
 }
  37. 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()
 }
 }
  38. 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()
 }
 }
  39. 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()
 }
 }
  40. 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()
 }
 }