Skip to content

Commit

Permalink
Fix tests using mockk instead of mockito, more prepared to work with …
Browse files Browse the repository at this point in the history
…Kotlin instead of Java
  • Loading branch information
davigonz committed Jul 5, 2019
1 parent 5e5348e commit f3fe2a0
Show file tree
Hide file tree
Showing 31 changed files with 423 additions and 558 deletions.
6 changes: 4 additions & 2 deletions owncloudApp/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ dependencies {
// Dependencies for unit tests
testImplementation "junit:junit:$junitVersion"
testImplementation "androidx.arch.core:core-testing:$archLifecycleVersion"
testImplementation "org.mockito:mockito-core:$mockitoVersion"
testImplementation "io.mockk:mockk:$mockkVersion"

// Dependencies for instrumented tests
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
Expand All @@ -50,7 +50,9 @@ dependencies {
androidTestImplementation 'androidx.annotation:annotation:1.1.0'
androidTestImplementation "androidx.room:room-testing:$roomVersion"
androidTestImplementation "androidx.arch.core:core-testing:$archLifecycleVersion"
androidTestImplementation "org.mockito:mockito-android:$mockitoVersion"
androidTestImplementation ("io.mockk:mockk-android:$mockkVersion") {
exclude module: 'objenesis'
}

// Image loading
implementation('com.github.bumptech.glide:glide:4.6.1') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import android.accounts.Account
import android.accounts.AccountManager
import android.content.Context
import android.content.Intent
import android.os.Parcelable
import androidx.lifecycle.MutableLiveData
import androidx.test.core.app.ApplicationProvider
import androidx.test.espresso.Espresso.onView
Expand Down Expand Up @@ -52,8 +51,7 @@ 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 io.mockk.mockk
import org.junit.AfterClass
import org.junit.Before
import org.junit.BeforeClass
Expand Down Expand Up @@ -100,8 +98,8 @@ class CreatePublicShareTest {
private val capabilitiesLiveData = MutableLiveData<Resource<OCCapabilityEntity>>()
private val sharesLiveData = MutableLiveData<Resource<List<OCShareEntity>>>()

private val ocCapabilityViewModel = mockkClass(OCCapabilityViewModel::class)
private val ocShareViewModel = mockkClass(OCShareViewModel::class)
private val ocCapabilityViewModel = mockk<OCCapabilityViewModel>(relaxed = true)
private val ocShareViewModel = mockk<OCShareViewModel>(relaxed = true)

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

@Before
fun setUp() {
val intent = spyk<Intent>()
val intent = Intent()

file = getOCFileForTesting("image.jpg")

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

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()
every { ocShareViewModel.getPrivateShares(file.remotePath) } returns MutableLiveData()

stopKoin()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import android.accounts.Account
import android.accounts.AccountManager
import android.content.Context
import android.content.Intent
import android.os.Parcelable
import androidx.lifecycle.MutableLiveData
import androidx.test.core.app.ApplicationProvider
import androidx.test.espresso.Espresso.onView
Expand Down Expand Up @@ -53,6 +52,8 @@ 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.mockk
import org.hamcrest.Matchers.allOf
import org.hamcrest.Matchers.not
import org.junit.AfterClass
Expand All @@ -65,10 +66,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.ArgumentMatchers
import org.mockito.Mockito.`when`
import org.mockito.Mockito.mock
import org.mockito.Mockito.spy

class DeletePublicShareTest {
@Rule
Expand Down Expand Up @@ -100,8 +97,8 @@ class DeletePublicShareTest {
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 = mockk<OCCapabilityViewModel>(relaxed = true)
private val ocShareViewModel = mockk<OCShareViewModel>(relaxed = true)

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

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

file = getOCFileForTesting("image.jpg")

`when`(intent.getParcelableExtra(FileActivity.EXTRA_FILE) as? Parcelable).thenReturn(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.getPrivateShares(file.remotePath) } returns MutableLiveData()

stopKoin()

Expand Down Expand Up @@ -195,13 +190,12 @@ class DeletePublicShareTest {
val existingPublicShare = publicShares.take(2) as ArrayList<OCShareEntity>
loadSharesSuccessfully(existingPublicShare)

`when`(
ocShareViewModel.deletePublicShare(ArgumentMatchers.anyLong())
).thenReturn(
MutableLiveData<Resource<Unit>>().apply {
postValue(Resource.success())
}
)
every {
ocShareViewModel.deletePublicShare(any())
} returns MutableLiveData<Resource<Unit>>().apply {
postValue(Resource.success())

}

onView(allOf(withId(R.id.deletePublicLinkButton), hasSibling(withText(existingPublicShare[0].name))))
.perform(click())
Expand All @@ -224,13 +218,11 @@ class DeletePublicShareTest {
val existingPublicShare = publicShares[0]
loadSharesSuccessfully(arrayListOf(existingPublicShare))

`when`(
ocShareViewModel.deletePublicShare(ArgumentMatchers.anyLong())
).thenReturn(
MutableLiveData<Resource<Unit>>().apply {
postValue(Resource.success())
}
)
every {
ocShareViewModel.deletePublicShare(any())
} returns MutableLiveData<Resource<Unit>>().apply {
postValue(Resource.success())
}

onView(withId(R.id.deletePublicLinkButton)).perform(click())
onView(withId(android.R.id.button1)).perform(click())
Expand Down Expand Up @@ -271,18 +263,16 @@ class DeletePublicShareTest {
val existingPublicShare = publicShares[0]
loadSharesSuccessfully(arrayListOf(existingPublicShare))

`when`(
ocShareViewModel.deletePublicShare(ArgumentMatchers.anyLong())
).thenReturn(
MutableLiveData<Resource<Unit>>().apply {
postValue(
Resource.error(
RemoteOperationResult.ResultCode.FORBIDDEN,
exception = Exception("Error when retrieving shares")
)
every {
ocShareViewModel.deletePublicShare(any())
} returns MutableLiveData<Resource<Unit>>().apply {
postValue(
Resource.error(
RemoteOperationResult.ResultCode.FORBIDDEN,
exception = Exception("Error when retrieving shares")
)
}
)
)
}

onView(withId(R.id.deletePublicLinkButton)).perform(click())
onView(withId(android.R.id.button1)).perform(click())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import android.accounts.Account
import android.accounts.AccountManager
import android.content.Context
import android.content.Intent
import android.os.Parcelable
import androidx.lifecycle.MutableLiveData
import androidx.test.core.app.ApplicationProvider
import androidx.test.espresso.Espresso.onView
Expand Down Expand Up @@ -54,6 +53,8 @@ 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.mockk
import org.junit.AfterClass
import org.junit.Before
import org.junit.BeforeClass
Expand All @@ -64,9 +65,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 EditPublicShareFolderTest {
@Rule
Expand Down Expand Up @@ -125,8 +123,8 @@ class EditPublicShareFolderTest {
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 = mockk<OCCapabilityViewModel>(relaxed = true)
private val ocShareViewModel = mockk<OCShareViewModel>(relaxed = true)

companion object {
private val targetContext = InstrumentationRegistry.getInstrumentation().targetContext
Expand Down Expand Up @@ -182,17 +180,15 @@ class EditPublicShareFolderTest {

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

file = getOCFileForTesting("Photos")

`when`(intent.getParcelableExtra(FileActivity.EXTRA_FILE) as? Parcelable).thenReturn(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.getPrivateShares(file.remotePath) } returns MutableLiveData()

stopKoin()

Expand Down Expand Up @@ -222,7 +218,7 @@ class EditPublicShareFolderTest {

val updatedPublicShare = publicShares[1]

`when`(
every {
ocShareViewModel.updatePublicShareForFile(
1,
updatedPublicShare.name!!,
Expand All @@ -231,11 +227,9 @@ class EditPublicShareFolderTest {
1,
false
)
).thenReturn(
MutableLiveData<Resource<Unit>>().apply {
postValue(Resource.success())
}
)
} returns MutableLiveData<Resource<Unit>>().apply {
postValue(Resource.success())
}

// 1. Open dialog to edit an existing public share
onView(withId(R.id.editPublicLinkButton)).perform(click())
Expand Down Expand Up @@ -266,7 +260,8 @@ class EditPublicShareFolderTest {
loadSharesSuccessfully(arrayListOf(existingPublicShare))

val updatedPublicShare = publicShares[2]
`when`(

every {
ocShareViewModel.updatePublicShareForFile(
1,
updatedPublicShare.name!!,
Expand All @@ -275,11 +270,9 @@ class EditPublicShareFolderTest {
15,
true
)
).thenReturn(
MutableLiveData<Resource<Unit>>().apply {
postValue(Resource.success())
}
)
} returns MutableLiveData<Resource<Unit>>().apply {
postValue(Resource.success())
}

// 1. Open dialog to edit an existing public share
onView(withId(R.id.editPublicLinkButton)).perform(click())
Expand Down Expand Up @@ -311,7 +304,8 @@ class EditPublicShareFolderTest {
loadSharesSuccessfully(arrayListOf(existingPublicShare))

val updatedPublicShare = publicShares[3]
`when`(

every {
ocShareViewModel.updatePublicShareForFile(
1,
updatedPublicShare.name!!,
Expand All @@ -320,11 +314,9 @@ class EditPublicShareFolderTest {
4,
true
)
).thenReturn(
MutableLiveData<Resource<Unit>>().apply {
postValue(Resource.success())
}
)
} returns MutableLiveData<Resource<Unit>>().apply {
postValue(Resource.success())
}

// 1. Open dialog to edit an existing public share
onView(withId(R.id.editPublicLinkButton)).perform(click())
Expand Down Expand Up @@ -356,7 +348,8 @@ class EditPublicShareFolderTest {
loadSharesSuccessfully(arrayListOf(existingPublicShare))

val updatedPublicShare = publicShares[0]
`when`(

every {
ocShareViewModel.updatePublicShareForFile(
1,
updatedPublicShare.name!!,
Expand All @@ -365,11 +358,9 @@ class EditPublicShareFolderTest {
1,
false
)
).thenReturn(
MutableLiveData<Resource<Unit>>().apply {
postValue(Resource.success())
}
)
} returns MutableLiveData<Resource<Unit>>().apply {
postValue(Resource.success())
}

// 1. Open dialog to edit an existing public share
onView(withId(R.id.editPublicLinkButton)).perform(click())
Expand Down
Loading

0 comments on commit f3fe2a0

Please sign in to comment.