Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,20 @@ android {
dependencies {
implementation(Support.supportLegacy)
testImplementation(Testing.junit)
testImplementation(Testing.mockServer)
testImplementation(Testing.coreTesting)
testImplementation(Testing.mockito)

testImplementation(Kotlin.coroutinesAndroid)
testImplementation(Testing.coroutinesTest)
androidTestImplementation(Kotlin.coroutinesAndroid)
androidTestImplementation(Testing.coroutinesTest)

androidTestImplementation(Testing.supportJunitExt)
androidTestImplementation(Testing.espressoCore)
androidTestImplementation(Testing.coreTesting)

implementation(ArchComponent.lifecycleExtension)

implementation(Kotlin.kotlinStd7)
implementation(Support.compat)
Expand Down
17 changes: 0 additions & 17 deletions app/src/test/java/com/worldsnas/starplayer/ExampleUnitTest.kt

This file was deleted.

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 app/src/test/java/com/worldsnas/starplayer/ViewModelUnitTest.kt
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 {
Comment thread
sourena21 marked this conversation as resolved.

@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>>
Comment thread
sourena21 marked this conversation as resolved.

@Mock
private lateinit var musicRepository: MusicRepository

@Before
fun setUp() {
Dispatchers.setMain(mainThreadSurrogate)
Comment thread
sourena21 marked this conversation as resolved.
}

@Test
fun testMusicListViewModel() {
testCoroutineRule.runBlockingTest {
Comment thread
sourena21 marked this conversation as resolved.
var musicListViewModel = MusicListViewModel(musicRepository)
Comment thread
sourena21 marked this conversation as resolved.
musicListViewModel.postMusic().observeForever(musicObserver)
Comment thread
sourena21 marked this conversation as resolved.
verify(musicRepository).getLocalData()
verify(musicObserver).onChanged(emptyList())
Comment thread
sourena21 marked this conversation as resolved.
}
}

@After
fun destroy() {
Dispatchers.resetMain()
Comment thread
sourena21 marked this conversation as resolved.
}
}
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) =
Comment thread
sourena21 marked this conversation as resolved.
testCoroutineScope.runBlockingTest { block() }

}
14 changes: 7 additions & 7 deletions buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ object Versions {
const val mockkVersion = "1.9.3"
const val fakeItVersion = "v0.5"

const val coreTestingVersion = "2.0.1"
const val mockitoVersion = "2.1.0"
const val mockServerVersion = "3.12.0"

const val kotlinVersion = "1.3.61"
const val coroutinesVersion = "1.3.3"
const val detektPluginVersion = "1.0.0-RC16"
Expand Down Expand Up @@ -109,30 +113,26 @@ object Testing {
//testing
const val junit = "junit:junit:${Versions.jUnitVersion}"
const val assertJ = "org.assertj:assertj-core:${Versions.assertJVersion}"

const val mockkUnit = "io.mockk:mockk:${Versions.mockkVersion}"
const val mockkAndroid = "io.mockk:mockk-android:${Versions.mockkVersion}"

const val mockServer = "com.squareup.okhttp3:mockwebserver:${Versions.mockServerVersion}"
const val coroutinesTest =
"org.jetbrains.kotlinx:kotlinx-coroutines-test:${Versions.coroutinesVersion}"

const val supportTestRunner = "androidx.test:runner:${Versions.supportTestVersion}"
const val supportTestCore = "androidx.test:core:${Versions.supportTestVersion}"
const val supportTestRule = "androidx.test:rules:${Versions.supportTestVersion}"
const val supportJunitExt = "androidx.test.ext:junit:${Versions.supportCoreVersion}"
const val supportCore = "androidx.test:core:${Versions.supportTestVersion}"

const val coreTesting = "androidx.arch.core:core-testing:${Versions.coreTestingVersion}"
const val espressoCore = "androidx.test.espresso:espresso-core:${Versions.espressoVersion}"
const val espressoIntents =
"androidx.test.espresso:espresso-intents:${Versions.espressoVersion}"
const val espressoContrib =
"androidx.test.espresso:espresso-contrib:${Versions.espressoVersion}"

const val jsonTest = "org.json:json:${Versions.jsonTestVersion}"

const val fakeIt = "com.github.moove-it:fakeit:${Versions.fakeItVersion}"

const val robolectric = "org.robolectric:robolectric:4.2"
const val mockito = "org.mockito:mockito-core:3.3.3"
}

object Kotlin {
Expand Down