Skip to content

Commit

Permalink
Start to use Mockk instead of Mockito since is better prepared to be …
Browse files Browse the repository at this point in the history
…used along with Kotlin
  • Loading branch information
davigonz committed Jul 3, 2019
1 parent 6e0c7f5 commit 0b6a08f
Show file tree
Hide file tree
Showing 15 changed files with 192 additions and 160 deletions.
5 changes: 3 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ buildscript {
archLifecycleVersion = '2.0.0'
roomVersion = '2.1.0'
koinVersion = '2.0.1'
mockkVersion = '1.9.3'

// Testing
junitVersion = "4.12"
mockitoVersion = "2.24.0"
junitVersion = '4.12'
mockitoVersion = '2.24.0'
}

repositories {
Expand Down
6 changes: 6 additions & 0 deletions owncloudApp/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ dependencies {

// Koin dependency injector
implementation "org.koin:koin-androidx-viewmodel:$koinVersion"

// Kotlin library to mock
testImplementation "io.mockk:mockk:$mockkVersion"
androidTestImplementation ("io.mockk:mockk-android:$mockkVersion") {
exclude module: 'objenesis'
}
}

tasks.withType(Test) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import com.owncloud.android.authentication.AccountAuthenticator.KEY_AUTH_TOKEN_T
import com.owncloud.android.data.Resource
import com.owncloud.android.data.capabilities.db.OCCapabilityEntity
import com.owncloud.android.data.sharing.shares.db.OCShareEntity
import com.owncloud.android.utils.AccountsManager
import com.owncloud.android.datamodel.OCFile
import com.owncloud.android.lib.common.accounts.AccountUtils
import com.owncloud.android.lib.common.operations.RemoteOperationResult
Expand All @@ -50,7 +49,11 @@ import com.owncloud.android.presentation.capabilities.OCCapabilityViewModel
import com.owncloud.android.presentation.sharing.shares.OCShareViewModel
import com.owncloud.android.presentation.sharing.shares.ShareActivity
import com.owncloud.android.ui.activity.FileActivity
import com.owncloud.android.utils.AccountsManager
import com.owncloud.android.utils.AppTestUtil
import io.mockk.every
import io.mockk.mockkClass
import io.mockk.spyk
import org.junit.AfterClass
import org.junit.Before
import org.junit.BeforeClass
Expand All @@ -61,9 +64,6 @@ import org.koin.androidx.viewmodel.dsl.viewModel
import org.koin.core.context.startKoin
import org.koin.core.context.stopKoin
import org.koin.dsl.module
import org.mockito.Mockito.`when`
import org.mockito.Mockito.mock
import org.mockito.Mockito.spy

class CreatePublicShareTest {
@Rule
Expand Down Expand Up @@ -100,8 +100,8 @@ class CreatePublicShareTest {
private val capabilitiesLiveData = MutableLiveData<Resource<OCCapabilityEntity>>()
private val sharesLiveData = MutableLiveData<Resource<List<OCShareEntity>>>()

private val ocCapabilityViewModel = mock(OCCapabilityViewModel::class.java)
private val ocShareViewModel = mock(OCShareViewModel::class.java)
private val ocCapabilityViewModel = mockkClass(OCCapabilityViewModel::class)
private val ocShareViewModel = mockkClass(OCShareViewModel::class)

companion object {
private val targetContext = InstrumentationRegistry.getInstrumentation().targetContext
Expand Down Expand Up @@ -157,17 +157,17 @@ class CreatePublicShareTest {

@Before
fun setUp() {
val intent = spy(Intent::class.java)
val intent = spyk<Intent>()

file = getOCFileForTesting("image.jpg")

`when`(intent.getParcelableExtra(FileActivity.EXTRA_FILE) as? Parcelable).thenReturn(file)
every { intent.getParcelableExtra(FileActivity.EXTRA_FILE) as? Parcelable } returns file
intent.putExtra(FileActivity.EXTRA_FILE, file)

`when`(ocCapabilityViewModel.getCapabilityForAccount(false)).thenReturn(capabilitiesLiveData)
`when`(ocCapabilityViewModel.getCapabilityForAccount(true)).thenReturn(capabilitiesLiveData)
`when`(ocShareViewModel.getPublicShares(file.remotePath)).thenReturn(sharesLiveData)
`when`(ocShareViewModel.getPrivateShares(file.remotePath)).thenReturn(MutableLiveData())
every { ocCapabilityViewModel.getCapabilityForAccount(false) } returns capabilitiesLiveData
every { ocCapabilityViewModel.getCapabilityForAccount(true) } returns capabilitiesLiveData
every { ocShareViewModel.getPublicShares(file.remotePath) } returns sharesLiveData
every { ocShareViewModel.getPublicShares(file.remotePath) } returns MutableLiveData()

stopKoin()

Expand Down Expand Up @@ -355,7 +355,8 @@ class CreatePublicShareTest {
}

private fun savePublicShare(newShare: OCShareEntity, resource: Resource<Unit> = Resource.success()) {
`when`(

every {
ocShareViewModel.insertPublicShare(
file.remotePath,
1,
Expand All @@ -364,11 +365,9 @@ class CreatePublicShareTest {
-1,
false
)
).thenReturn(
MutableLiveData<Resource<Unit>>().apply {
postValue(resource)
}
)
} returns MutableLiveData<Resource<Unit>>().apply {
postValue(resource)
}

onView(withId(R.id.saveButton)).perform(click())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ import com.owncloud.android.data.capabilities.db.OCCapabilityEntity
import com.owncloud.android.domain.capabilities.OCCapabilityRepository
import com.owncloud.android.presentation.capabilities.OCCapabilityViewModel
import com.owncloud.android.utils.AppTestUtil
import io.mockk.every
import io.mockk.mockkClass
import junit.framework.Assert.assertEquals
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.mockito.Mockito.`when`
import org.mockito.Mockito.mock

@RunWith(JUnit4::class)
class OCCapabilityViewModelTest {
Expand All @@ -53,17 +53,15 @@ class OCCapabilityViewModelTest {

@Test
fun loadCapability() {
val ocCapabilityRepository = mock(OCCapabilityRepository::class.java)
val ocCapabilityRepository = mockkClass(OCCapabilityRepository::class)

`when`(
every {
ocCapabilityRepository.getCapabilityForAccount(
"admin@server"
)
).thenReturn(
MutableLiveData<Resource<OCCapabilityEntity>>().apply {
value = Resource.success(capability)
}
)
} returns MutableLiveData<Resource<OCCapabilityEntity>>().apply {
value = Resource.success(capability)
}

val context = InstrumentationRegistry.getInstrumentation().targetContext

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ import com.owncloud.android.lib.resources.shares.GetRemoteShareesOperation
import com.owncloud.android.lib.resources.shares.ShareType
import com.owncloud.android.presentation.sharing.sharees.OCShareeViewModel
import com.owncloud.android.utils.AppTestUtil
import io.mockk.every
import io.mockk.mockkClass
import junit.framework.Assert.assertEquals
import org.json.JSONObject
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.mockito.Mockito.`when`
import org.mockito.Mockito.mock

@RunWith(JUnit4::class)
class OCShareeViewmodelTest {
Expand All @@ -44,7 +44,7 @@ class OCShareeViewmodelTest {
val instantExecutorRule = InstantTaskExecutorRule()

private var testAccount: Account = AppTestUtil.createAccount("admin@server", "test")
private var ocShareeRepository: OCShareeRepository = mock(OCShareeRepository::class.java)
private var ocShareeRepository: OCShareeRepository = mockkClass(OCShareeRepository::class)

@Test
fun loadSharees() {
Expand All @@ -53,11 +53,11 @@ class OCShareeViewmodelTest {
AppTestUtil.createSharee("Group", ShareType.GROUP.value.toString(), "user2", "[email protected]")
)

`when`(
every {
ocShareeRepository.getSharees(
"User", 1, 10
)
).thenReturn(Resource.success(sharees))
} returns Resource.success(sharees)

val ocShareeViewModel = createOCShareeViewModel(ocShareeRepository)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ import com.owncloud.android.lib.resources.status.OwnCloudVersion
import com.owncloud.android.presentation.sharing.sharees.SearchShareesFragment
import com.owncloud.android.presentation.sharing.shares.views.TestShareFileActivity
import com.owncloud.android.utils.AppTestUtil
import io.mockk.every
import io.mockk.mockkClass
import org.hamcrest.CoreMatchers
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito
import org.mockito.Mockito.`when`

@RunWith(AndroidJUnit4::class)
class SearchShareesFragmentTest {
Expand Down Expand Up @@ -113,9 +113,9 @@ class SearchShareesFragmentTest {
capabilities: OCCapabilityEntity = AppTestUtil.createCapability(),
privateShares: ArrayList<OCShareEntity> = arrayListOf()
) {
val account = Mockito.mock(Account::class.java)
val ownCloudVersion = Mockito.mock(OwnCloudVersion::class.java)
`when`(ownCloudVersion.isSearchUsersSupported).thenReturn(true)
val account = mockkClass(Account::class)
val ownCloudVersion = mockkClass(OwnCloudVersion::class)
every { ownCloudVersion.isSearchUsersSupported } returns true

val searchShareesFragment =
SearchShareesFragment.newInstance(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,16 @@ import com.owncloud.android.data.Resource
import com.owncloud.android.data.Status
import com.owncloud.android.data.sharing.shares.db.OCShareEntity
import com.owncloud.android.domain.sharing.shares.OCShareRepository
import com.owncloud.android.domain.utils.DomainTestUtil
import com.owncloud.android.lib.resources.shares.ShareType
import com.owncloud.android.presentation.sharing.shares.OCShareViewModel
import com.owncloud.android.utils.AppTestUtil
import junit.framework.Assert.assertEquals
import io.mockk.every
import io.mockk.mockkClass
import org.junit.Assert.assertEquals
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.mockito.Mockito.`when`
import org.mockito.Mockito.mock

@RunWith(JUnit4::class)
class OCShareViewModelTest {
Expand All @@ -48,7 +47,7 @@ class OCShareViewModelTest {
private val filePath = "/Photos/image.jpg"

private var testAccount: Account = AppTestUtil.createAccount("admin@server", "test")
private var ocShareRepository: OCShareRepository = DomainTestUtil.createOCShareRepositoryMock()
private var ocShareRepository: OCShareRepository = mockkClass(OCShareRepository::class)

/******************************************************************************************************
******************************************* PRIVATE SHARES *******************************************
Expand All @@ -71,13 +70,10 @@ class OCShareViewModelTest {
)
)

`when`(
ocShareRepository.getPrivateShares(filePath)
).thenReturn(
MutableLiveData<Resource<List<OCShareEntity>>>().apply {
value = Resource.success(privateShares)
}
)
every { ocShareRepository.getPrivateShares(filePath) } returns
MutableLiveData<Resource<List<OCShareEntity>>>().apply {
value = Resource.success(privateShares)
}

// Viewmodel that will ask ocShareRepository for shares
val ocShareViewModel = createOCShareViewModel(ocShareRepository)
Expand All @@ -88,20 +84,16 @@ class OCShareViewModelTest {

@Test
fun insertPrivateShare() {
val ocShareRepository = mock(OCShareRepository::class.java)

`when`(
every {
ocShareRepository.insertPrivateShare(
filePath,
ShareType.GROUP,
"user",
-1
)
).thenReturn(
MutableLiveData<Resource<Unit>>().apply {
value = Resource.success()
}
)
} returns MutableLiveData<Resource<Unit>>().apply {
value = Resource.success()
}

// Viewmodel that will ask ocShareRepository for shares
val ocShareViewModel = createOCShareViewModel(ocShareRepository)
Expand Down Expand Up @@ -147,13 +139,10 @@ class OCShareViewModelTest {
)
)

`when`(
ocShareRepository.getPublicShares(filePath)
).thenReturn(
MutableLiveData<Resource<List<OCShareEntity>>>().apply {
value = Resource.success(publicShares)
}
)
every { ocShareRepository.getPublicShares(filePath) } returns
MutableLiveData<Resource<List<OCShareEntity>>>().apply {
value = Resource.success(publicShares)
}

// Viewmodel that will ask ocShareRepository for shares
val ocShareViewModel = createOCShareViewModel(ocShareRepository)
Expand All @@ -164,7 +153,7 @@ class OCShareViewModelTest {

@Test
fun insertPublicShare() {
`when`(
every {
ocShareRepository.insertPublicShare(
filePath,
1,
Expand All @@ -173,11 +162,9 @@ class OCShareViewModelTest {
-1,
false
)
).thenReturn(
MutableLiveData<Resource<Unit>>().apply {
value = Resource.success()
}
)
} returns MutableLiveData<Resource<Unit>>().apply {
value = Resource.success()
}

// Viewmodel that will ask ocShareRepository for shares
val ocShareViewModel = createOCShareViewModel(ocShareRepository)
Expand All @@ -196,7 +183,7 @@ class OCShareViewModelTest {

@Test
fun updatePublicShare() {
`when`(
every {
ocShareRepository.updatePublicShare(
1,
"Photos 1 link",
Expand All @@ -205,11 +192,9 @@ class OCShareViewModelTest {
1,
false
)
).thenReturn(
MutableLiveData<Resource<Unit>>().apply {
value = Resource.success()
}
)
} returns MutableLiveData<Resource<Unit>>().apply {
value = Resource.success()
}

// Viewmodel that will ask ocShareRepository for shares
val ocShareViewModel = createOCShareViewModel(ocShareRepository)
Expand All @@ -228,15 +213,13 @@ class OCShareViewModelTest {

@Test
fun deletePublicShare() {
`when`(
every {
ocShareRepository.deletePublicShare(
3
)
).thenReturn(
MutableLiveData<Resource<Unit>>().apply {
value = Resource.success()
}
)
} returns MutableLiveData<Resource<Unit>>().apply {
value = Resource.success()
}

// Viewmodel that will ask ocShareRepository for shares
val ocShareViewModel = createOCShareViewModel(ocShareRepository)
Expand Down
Loading

0 comments on commit 0b6a08f

Please sign in to comment.