Last active
April 4, 2019 16:36
-
-
Save marinat/37683e38d40052d90639d85f6a568bf8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RunWith(MockitoJUnitRunner::class) | |
class SampleUnitTest { | |
lateinit var syncUseCase: SyncUseCaseImpl | |
lateinit var mdr: MusicDataRepository | |
lateinit var plr: TestPlayListRepositoryImpl | |
@Before | |
fun createUseCase() { | |
MockitoAnnotations.initMocks(this) | |
mdr = Mockito.mock(MusicDataRepository::class.java) | |
plr = TestPlayListRepositoryImpl() | |
runBlocking { | |
Mockito.`when`(mdr.getMusicData()).thenReturn(MusicData(ArrayList(), arrayListOf( | |
PlayList(0, "pl1", ""), | |
PlayList(1, "pl2", ""), | |
PlayList(2, "pl3", ""), | |
PlayList(3, "pl4", "") | |
), ArrayList())) | |
} | |
syncUseCase = SyncUseCaseImpl(mdr, plr) | |
} | |
@Test | |
fun mdrTest() { | |
plr.playLists = arrayListOf( | |
PlayList(0, "pl1", ""), | |
PlayList(1, "pl2", "") | |
) | |
runBlocking { | |
syncUseCase.syncMusicData() | |
assert(plr.getAll().size == 4) | |
} | |
} | |
class TestPlayListRepositoryImpl : PlayListRepository { | |
var playLists = ArrayList<PlayList>() | |
override suspend fun getById(id: Long): PlayList? { | |
TODO("not implemented") //To change body of created functions use File | Settings | File Templates. | |
} | |
override suspend fun insert(pl: PlayList) { | |
TODO("not implemented") //To change body of created functions use File | Settings | File Templates. | |
} | |
override suspend fun insertAll(playLists: List<PlayList>) { | |
this.playLists.addAll(playLists) | |
} | |
override suspend fun getAll(): List<PlayList> { | |
return playLists | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override suspend fun syncMusicData() { | |
val data = musicDataRepository.getMusicData() | |
val playLists = playListRepository.getAll() | |
val playListsToInsert = ArrayList<PlayList>() | |
for (playList in data.playLists) { | |
if (playLists.none { it.id == playList.id }) { | |
playListsToInsert.add(playList) | |
} | |
if (playLists.contains(playList)) { | |
continue | |
} | |
} | |
System.out.println("lists to add = $playListsToInsert") | |
playListRepository.insertAll(playListsToInsert) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Очень много кода, а тест один и ассерт тоже один