-
Notifications
You must be signed in to change notification settings - Fork 4
add first test for MusicListViewModel #41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sourena21
wants to merge
6
commits into
dev
Choose a base branch
from
add_test_viewmodels
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0711132
in adding test for MusicListViewModel
sourena21 0ea7084
complete first test for MusicListViewModel (call method postMusic())
sourena21 4ac0f82
start add test for LocalMusicProviderImpl
sourena21 cdeb96e
start add test for LocalMusicProviderImpl
sourena21 0c5d0fa
start add test for LocalMusicProviderImpl
sourena21 675d5d4
start add test for LocalMusicProviderImpl
sourena21 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
17 changes: 0 additions & 17 deletions
17
app/src/test/java/com/worldsnas/starplayer/ExampleUnitTest.kt
This file was deleted.
Oops, something went wrong.
24 changes: 24 additions & 0 deletions
24
app/src/test/java/com/worldsnas/starplayer/LocalMusicProviderTest.kt
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package com.worldsnas.starplayer | ||
|
|
||
| import android.content.ContentResolver | ||
| import com.worldsnas.starplayer.model.LocalMusicProviderImpl | ||
| import kotlinx.coroutines.test.TestCoroutineDispatcher | ||
| import kotlinx.coroutines.test.TestCoroutineScope | ||
| import kotlinx.coroutines.test.runBlockingTest | ||
| import org.junit.Assert | ||
| import org.junit.Test | ||
| import org.mockito.Mockito.mock | ||
|
|
||
| class LocalMusicProviderTest { | ||
|
|
||
| private val testCoroutineDispatcher = TestCoroutineDispatcher() | ||
| private val testCoroutineScope = TestCoroutineScope(testCoroutineDispatcher) | ||
|
|
||
| @Test | ||
| fun testLocalMusicProviderImpl() =testCoroutineScope.runBlockingTest{ | ||
| // var contentResolver = mock<ContentResolver>() | ||
| // var localMusicProviderImpl = LocalMusicProviderImpl(contentResolver) | ||
| // var localMusicList=localMusicProviderImpl.getAllMusic() | ||
| // Assert.assertNotNull(localMusicList) | ||
| } | ||
| } |
69 changes: 69 additions & 0 deletions
69
app/src/test/java/com/worldsnas/starplayer/ViewModelUnitTest.kt
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| package com.worldsnas.starplayer | ||
|
|
||
| import TestCoroutineRule | ||
| import android.content.ContentResolver | ||
| import androidx.arch.core.executor.testing.InstantTaskExecutorRule | ||
| import androidx.lifecycle.Observer | ||
| import com.worldsnas.starplayer.model.LocalMusicProviderImpl | ||
| import com.worldsnas.starplayer.model.MusicRepoModel | ||
| import com.worldsnas.starplayer.model.MusicRepository | ||
| import com.worldsnas.starplayer.view.musics_list.MusicListViewModel | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
| import kotlinx.coroutines.newSingleThreadContext | ||
| import kotlinx.coroutines.test.* | ||
| import org.junit.* | ||
| import org.junit.rules.TestRule | ||
|
|
||
| import org.junit.runner.RunWith | ||
| import org.junit.runners.JUnit4 | ||
| import org.mockito.ArgumentMatchers.any | ||
| import org.mockito.Mock | ||
| import org.mockito.Mockito.mock | ||
| import org.mockito.Mockito.verify | ||
| import org.mockito.junit.MockitoJUnitRunner | ||
|
|
||
| /* | ||
| * Example local unit test, which will execute on the development machine (host). | ||
| * | ||
| * See [testing documentation](http://d.android.com/tools/testing). | ||
| */ | ||
|
|
||
| @ExperimentalCoroutinesApi | ||
| @RunWith(MockitoJUnitRunner::class) | ||
| class ViewModelUnitTest { | ||
|
|
||
| @get:Rule | ||
| val testInstantTaskExecutorRule = InstantTaskExecutorRule() | ||
|
|
||
| @get:Rule | ||
| val testCoroutineRule = TestCoroutineRule() | ||
|
|
||
| private val mainThreadSurrogate = newSingleThreadContext("Mocked UI thread") | ||
|
|
||
| @Mock | ||
| private lateinit var musicObserver: Observer<List<MusicRepoModel>> | ||
|
sourena21 marked this conversation as resolved.
|
||
|
|
||
| @Mock | ||
| private lateinit var musicRepository: MusicRepository | ||
|
|
||
| @Before | ||
| fun setUp() { | ||
| Dispatchers.setMain(mainThreadSurrogate) | ||
|
sourena21 marked this conversation as resolved.
|
||
| } | ||
|
|
||
| @Test | ||
| fun testMusicListViewModel() { | ||
| testCoroutineRule.runBlockingTest { | ||
|
sourena21 marked this conversation as resolved.
|
||
| var musicListViewModel = MusicListViewModel(musicRepository) | ||
|
sourena21 marked this conversation as resolved.
|
||
| musicListViewModel.postMusic().observeForever(musicObserver) | ||
|
sourena21 marked this conversation as resolved.
|
||
| verify(musicRepository).getLocalData() | ||
| verify(musicObserver).onChanged(emptyList()) | ||
|
sourena21 marked this conversation as resolved.
|
||
| } | ||
| } | ||
|
|
||
| @After | ||
| fun destroy() { | ||
| Dispatchers.resetMain() | ||
|
sourena21 marked this conversation as resolved.
|
||
| } | ||
| } | ||
31 changes: 31 additions & 0 deletions
31
app/src/test/java/com/worldsnas/starplayer/utils/TestCoroutineRule.kt
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
|
|
||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
| import kotlinx.coroutines.test.* | ||
| import org.junit.rules.TestRule | ||
| import org.junit.runner.Description | ||
| import org.junit.runners.model.Statement | ||
|
|
||
| @ExperimentalCoroutinesApi | ||
| class TestCoroutineRule : TestRule { | ||
|
|
||
| private val testCoroutineDispatcher = TestCoroutineDispatcher() | ||
|
|
||
| private val testCoroutineScope = TestCoroutineScope(testCoroutineDispatcher) | ||
|
|
||
| override fun apply(base: Statement, description: Description?) = object : Statement() { | ||
| @Throws(Throwable::class) | ||
| override fun evaluate() { | ||
| Dispatchers.setMain(testCoroutineDispatcher) | ||
|
|
||
| base.evaluate() | ||
|
|
||
| Dispatchers.resetMain() | ||
| testCoroutineScope.cleanupTestCoroutines() | ||
| } | ||
| } | ||
|
|
||
| fun runBlockingTest(block: suspend TestCoroutineScope.() -> Unit) = | ||
|
sourena21 marked this conversation as resolved.
|
||
| testCoroutineScope.runBlockingTest { block() } | ||
|
|
||
| } | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.