mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-27 21:03:54 +01:00
Fix unit tests (#5947)
* move createLocale() method to companion object and add test dependency * use mockk() from Mockk library for mocking sealed classes * change method parameter to null-able String type * add null check for accessing property from unit tests * change method signature to match old method's signature It fixes the NullPointerException when running ImageProcessingUnitTest * Fix unresolved references and make properties public for unit tests * fix tests in UploadRepositoryUnitTest by making return type null-able
This commit is contained in:
parent
fe347c21fd
commit
e070c5dbe8
14 changed files with 56 additions and 52 deletions
|
|
@ -16,6 +16,7 @@ import fr.free.nrw.commons.TestCommonsApplication
|
|||
import fr.free.nrw.commons.auth.login.LoginResult
|
||||
import fr.free.nrw.commons.createTestClient
|
||||
import fr.free.nrw.commons.kvstore.JsonKvStore
|
||||
import io.mockk.mockk
|
||||
import org.junit.Assert
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
|
|
@ -66,11 +67,11 @@ class LoginActivityUnitTests {
|
|||
@Mock
|
||||
private lateinit var account: Account
|
||||
|
||||
@Mock
|
||||
private lateinit var loginResult: LoginResult
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
loginResult = mockk()
|
||||
MockitoAnnotations.openMocks(this)
|
||||
OkHttpConnectionFactory.CLIENT = createTestClient()
|
||||
activity = Robolectric.buildActivity(LoginActivity::class.java).create().get()
|
||||
|
|
|
|||
|
|
@ -7,13 +7,14 @@ import androidx.test.core.app.ApplicationProvider
|
|||
import fr.free.nrw.commons.TestCommonsApplication
|
||||
import fr.free.nrw.commons.auth.login.LoginResult
|
||||
import fr.free.nrw.commons.kvstore.JsonKvStore
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import org.junit.Assert
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mockito.Mock
|
||||
import org.mockito.MockitoAnnotations
|
||||
import org.powermock.api.mockito.PowerMockito.`when`
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.Shadows.shadowOf
|
||||
import org.robolectric.annotation.Config
|
||||
|
|
@ -33,7 +34,6 @@ class SessionManagerUnitTests {
|
|||
@Mock
|
||||
private lateinit var defaultKvStore: JsonKvStore
|
||||
|
||||
@Mock
|
||||
private lateinit var loginResult: LoginResult
|
||||
|
||||
@Mock
|
||||
|
|
@ -41,6 +41,7 @@ class SessionManagerUnitTests {
|
|||
|
||||
@Before
|
||||
fun setUp() {
|
||||
loginResult = mockk()
|
||||
MockitoAnnotations.openMocks(this)
|
||||
accountManager = AccountManager.get(ApplicationProvider.getApplicationContext())
|
||||
shadowOf(accountManager).addAccount(account)
|
||||
|
|
@ -68,8 +69,8 @@ class SessionManagerUnitTests {
|
|||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testUpdateAccount() {
|
||||
`when`(loginResult.userName).thenReturn("username")
|
||||
`when`(loginResult.password).thenReturn("password")
|
||||
every { loginResult.userName } returns "username"
|
||||
every { loginResult.password } returns "password"
|
||||
val method: Method =
|
||||
SessionManager::class.java.getDeclaredMethod(
|
||||
"updateAccount",
|
||||
|
|
|
|||
|
|
@ -85,7 +85,7 @@ class RecentLanguagesDaoUnitTest {
|
|||
whenever(client.query(any(), any(), anyOrNull(), any(), anyOrNull()))
|
||||
.thenReturn(createCursor(14))
|
||||
|
||||
val result = testObject.recentLanguages
|
||||
val result = testObject.getRecentLanguages()
|
||||
|
||||
Assert.assertEquals(14, (result.size))
|
||||
}
|
||||
|
|
@ -95,20 +95,20 @@ class RecentLanguagesDaoUnitTest {
|
|||
whenever(client.query(any(), any(), anyOrNull(), any(), anyOrNull())).thenThrow(
|
||||
RemoteException(""),
|
||||
)
|
||||
testObject.recentLanguages
|
||||
testObject.getRecentLanguages()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun getGetRecentLanguagesReturnsEmptyList_emptyCursor() {
|
||||
whenever(client.query(any(), any(), anyOrNull(), any(), anyOrNull()))
|
||||
.thenReturn(createCursor(0))
|
||||
Assert.assertTrue(testObject.recentLanguages.isEmpty())
|
||||
Assert.assertTrue(testObject.getRecentLanguages().isEmpty())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun getGetRecentLanguagesReturnsEmptyList_nullCursor() {
|
||||
whenever(client.query(any(), any(), anyOrNull(), any(), anyOrNull())).thenReturn(null)
|
||||
Assert.assertTrue(testObject.recentLanguages.isEmpty())
|
||||
Assert.assertTrue(testObject.getRecentLanguages().isEmpty())
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -117,7 +117,7 @@ class RecentLanguagesDaoUnitTest {
|
|||
whenever(client.query(any(), any(), anyOrNull(), any(), anyOrNull())).thenReturn(mockCursor)
|
||||
whenever(mockCursor.moveToFirst()).thenReturn(false)
|
||||
|
||||
testObject.recentLanguages
|
||||
testObject.getRecentLanguages()
|
||||
|
||||
verify(mockCursor).close()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ import fr.free.nrw.commons.TestCommonsApplication
|
|||
import fr.free.nrw.commons.recentlanguages.Language
|
||||
import fr.free.nrw.commons.recentlanguages.RecentLanguagesAdapter
|
||||
import fr.free.nrw.commons.recentlanguages.RecentLanguagesDao
|
||||
import fr.free.nrw.commons.settings.SettingsFragment.createLocale
|
||||
import fr.free.nrw.commons.settings.SettingsFragment.Companion.createLocale
|
||||
import org.junit.Assert
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Before
|
||||
|
|
@ -156,14 +156,14 @@ class SettingsFragmentUnitTests {
|
|||
)
|
||||
method.isAccessible = true
|
||||
method.invoke(fragment, "appUiDefaultLanguagePref")
|
||||
verify(recentLanguagesDao, times(1)).recentLanguages
|
||||
verify(recentLanguagesDao, times(1)).getRecentLanguages()
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun `Test prepareAppLanguages when recently used languages is not empty`() {
|
||||
Shadows.shadowOf(Looper.getMainLooper()).idle()
|
||||
whenever(recentLanguagesDao.recentLanguages)
|
||||
whenever(recentLanguagesDao.getRecentLanguages())
|
||||
.thenReturn(
|
||||
mutableListOf(
|
||||
Language("English", "en"),
|
||||
|
|
@ -181,7 +181,7 @@ class SettingsFragmentUnitTests {
|
|||
)
|
||||
method.isAccessible = true
|
||||
method.invoke(fragment, "appUiDefaultLanguagePref")
|
||||
verify(recentLanguagesDao, times(2)).recentLanguages
|
||||
verify(recentLanguagesDao, times(2)).getRecentLanguages()
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
|||
|
|
@ -131,7 +131,7 @@ class UploadMediaPresenterTest {
|
|||
*/
|
||||
@Test
|
||||
fun getImageQualityTest() {
|
||||
whenever(repository.uploads).thenReturn(listOf(uploadItem))
|
||||
whenever(repository.getUploads()).thenReturn(listOf(uploadItem))
|
||||
whenever(repository.getImageQuality(uploadItem, location))
|
||||
.thenReturn(testSingleImageResult)
|
||||
whenever(uploadItem.imageQuality).thenReturn(0)
|
||||
|
|
@ -149,7 +149,7 @@ class UploadMediaPresenterTest {
|
|||
*/
|
||||
@Test
|
||||
fun `get ImageQuality Test while coordinates equals to null`() {
|
||||
whenever(repository.uploads).thenReturn(listOf(uploadItem))
|
||||
whenever(repository.getUploads()).thenReturn(listOf(uploadItem))
|
||||
whenever(repository.getImageQuality(uploadItem, location))
|
||||
.thenReturn(testSingleImageResult)
|
||||
whenever(uploadItem.imageQuality).thenReturn(0)
|
||||
|
|
@ -225,7 +225,7 @@ class UploadMediaPresenterTest {
|
|||
*/
|
||||
@Test
|
||||
fun fetchImageAndTitleTest() {
|
||||
whenever(repository.uploads).thenReturn(listOf(uploadItem))
|
||||
whenever(repository.getUploads()).thenReturn(listOf(uploadItem))
|
||||
whenever(repository.getUploadItem(ArgumentMatchers.anyInt()))
|
||||
.thenReturn(uploadItem)
|
||||
whenever(uploadItem.uploadMediaDetails).thenReturn(listOf())
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ import fr.free.nrw.commons.repository.UploadRepository
|
|||
import fr.free.nrw.commons.upload.structure.depictions.DepictModel
|
||||
import fr.free.nrw.commons.upload.structure.depictions.DepictedItem
|
||||
import io.reactivex.Completable
|
||||
import io.reactivex.Observable
|
||||
import io.reactivex.Single
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
|
|
@ -196,7 +197,7 @@ class UploadRepositoryUnitTest {
|
|||
fun testGetCaptionQuality() {
|
||||
assertEquals(
|
||||
repository.getCaptionQuality(uploadItem),
|
||||
uploadModel.getCaptionQuality(uploadItem),
|
||||
uploadModel.getCaptionQuality(uploadItem)
|
||||
)
|
||||
}
|
||||
|
||||
|
|
@ -386,7 +387,8 @@ class UploadRepositoryUnitTest {
|
|||
fun testGetCategories() {
|
||||
assertEquals(
|
||||
repository.getCategories(listOf("Test")),
|
||||
categoriesModel.getCategoriesByName(mutableListOf("Test")),
|
||||
categoriesModel.getCategoriesByName(mutableListOf("Test"))
|
||||
?: Observable.empty<List<CategoryItem>>()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
package fr.free.nrw.commons.upload.structure.depictions
|
||||
|
||||
import com.nhaarman.mockitokotlin2.mock
|
||||
import depictedItem
|
||||
import entity
|
||||
import entityId
|
||||
import fr.free.nrw.commons.wikidata.WikidataProperties
|
||||
import io.mockk.mockk
|
||||
import org.junit.Assert
|
||||
import org.junit.Test
|
||||
import place
|
||||
|
|
@ -53,7 +53,7 @@ class DepictedItemTest {
|
|||
entity(
|
||||
statements =
|
||||
mapOf(
|
||||
WikidataProperties.IMAGE.propertyName to listOf(statement(snak(dataValue = mock()))),
|
||||
WikidataProperties.IMAGE.propertyName to listOf(statement(snak(dataValue = mockk()))),
|
||||
),
|
||||
),
|
||||
).imageUrl,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue