Skip to content

Instantly share code, notes, and snippets.

@marinat
Last active April 4, 2019 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marinat/37683e38d40052d90639d85f6a568bf8 to your computer and use it in GitHub Desktop.
Save marinat/37683e38d40052d90639d85f6a568bf8 to your computer and use it in GitHub Desktop.
@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
}
}
}
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)
}
@ioncreature
Copy link

Очень много кода, а тест один и ассерт тоже один

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment