Skip to content

Commit

Permalink
fixes unit tests (#5354)
Browse files Browse the repository at this point in the history
* fixes unit tests

* fixes failing unit tests
  • Loading branch information
srishti-R authored Oct 20, 2023
1 parent 988b83d commit c9dfc03
Show file tree
Hide file tree
Showing 122 changed files with 328 additions and 287 deletions.
21 changes: 11 additions & 10 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,22 @@ dependencies {

//Mocking
testImplementation 'com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0'
testImplementation 'org.mockito:mockito-inline:2.13.0'
testImplementation 'org.mockito:mockito-core:2.25.1'
testImplementation "org.powermock:powermock-module-junit4:2.0.2"
testImplementation "org.powermock:powermock-api-mockito2:2.0.2"
testImplementation 'org.mockito:mockito-inline:5.2.0'
testImplementation 'org.mockito:mockito-core:5.5.0'
testImplementation "org.powermock:powermock-module-junit4:2.0.9"
testImplementation "org.powermock:powermock-api-mockito2:2.0.9"

// Unit testing
testImplementation 'junit:junit:4.13.2'
testImplementation 'org.robolectric:robolectric:4.10.3'
testImplementation 'androidx.test:core:1.4.0'
testImplementation 'androidx.test:core:1.5.0'
testImplementation "com.squareup.okhttp3:mockwebserver:$OKHTTP_VERSION"
testImplementation "com.jraska.livedata:testing-ktx:1.1.2"
testImplementation "androidx.arch.core:core-testing:2.1.0"
testImplementation "org.junit.jupiter:junit-jupiter-api:5.7.0"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.7.0"
testImplementation "androidx.arch.core:core-testing:2.2.0"
testImplementation "org.junit.jupiter:junit-jupiter-api:5.10.0"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.10.0"
testImplementation 'com.facebook.soloader:soloader:0.10.1'
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.5.0"
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.3"

// Android testing
androidTestImplementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$KOTLIN_VERSION"
Expand All @@ -121,7 +121,6 @@ dependencies {
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation "androidx.exifinterface:exifinterface:1.3.2"
implementation "androidx.core:core-ktx:$CORE_KTX_VERSION"
implementation "androidx.multidex:multidex:2.0.1"
implementation 'com.simplecityapps:recyclerview-fastscroll:2.0.1'

//swipe_layout
Expand Down Expand Up @@ -153,6 +152,8 @@ dependencies {
//Glide
implementation 'com.github.bumptech.glide:glide:4.12.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
kaptTest "androidx.databinding:databinding-compiler:8.0.2"
kaptAndroidTest "androidx.databinding:databinding-compiler:8.0.2"

implementation("io.github.coordinates2country:coordinates2country-android:1.3") { exclude group: 'com.google.android', module: 'android' }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class AboutActivityUnitTests {

@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
MockitoAnnotations.openMocks(this)

activity = Robolectric.buildActivity(AboutActivity::class.java).create().get()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class CommonsAppAdapterUnitTest {

@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
MockitoAnnotations.openMocks(this)
adapter = CommonsAppAdapter(sessionManager, preferences)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class FakeContextWrapper(base: Context?) : ContextWrapper(base) {
}

init {
MockitoAnnotations.initMocks(this)
MockitoAnnotations.openMocks(this)
Mockito.`when`(mMockAccountManager.accounts).thenReturn(ACCOUNTS)
Mockito.`when`(mMockAccountManager.getAccountsByType(BuildConfig.ACCOUNT_TYPE))
.thenReturn(ACCOUNTS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class FakeContextWrapperWithException(base: Context?) : ContextWrapper(base) {
}

init {
MockitoAnnotations.initMocks(this)
MockitoAnnotations.openMocks(this)
Mockito.`when`(mMockAccountManager.getAccountsByType(BuildConfig.ACCOUNT_TYPE))
.thenThrow(SecurityException("Permission Denied"))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class MediaDataExtractorTest {
@Before
@Throws(Exception::class)
fun setUp() {
MockitoAnnotations.initMocks(this)
MockitoAnnotations.openMocks(this)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class OkHttpJsonApiClientTests {

@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
MockitoAnnotations.openMocks(this)
okHttpJsonApiClient = OkHttpJsonApiClient(
okhttpClient,
depictsClient,
Expand Down
24 changes: 24 additions & 0 deletions app/src/test/kotlin/fr/free/nrw/commons/TestUtility.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package fr.free.nrw.commons

import java.lang.reflect.Field
import java.lang.reflect.Modifier

object TestUtility {
@Throws(java.lang.Exception::class)
fun setFinalStatic(field: Field, newValue: Any?) {
try {
field.isAccessible = true
// remove final modifier from field
val modifiersField: Field = Field::class.java.getDeclaredField("modifiers")
modifiersField.isAccessible = true
modifiersField.setInt(field, field.modifiers and Modifier.FINAL.inv())
field.set(null, newValue)
} catch (e: SecurityException) {
e.stackTrace
} catch (e: NoSuchFieldException) {
e.stackTrace
} catch (e: java.lang.Exception) {
e.stackTrace
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class PageEditClientTest {
@Before
@Throws(Exception::class)
fun setUp() {
MockitoAnnotations.initMocks(this)
MockitoAnnotations.openMocks(this)
pageEditClient = PageEditClient(csrfTokenClient, pageEditInterface)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers
import org.mockito.Mock
import org.mockito.MockedStatic
import org.mockito.Mockito
import org.mockito.Mockito.`when`
import org.mockito.MockitoAnnotations
import org.powermock.api.mockito.PowerMockito
import org.powermock.core.classloader.annotations.PrepareForTest
import org.powermock.modules.junit4.PowerMockRunner
import org.robolectric.RobolectricTestRunner
import org.wikipedia.csrf.CsrfTokenClient
import org.wikipedia.dataclient.Service

@RunWith(PowerMockRunner::class)
@RunWith(RobolectricTestRunner::class)
@PrepareForTest(CommonsApplication::class)
class ThanksClientTest {
@Mock
Expand All @@ -28,16 +31,17 @@ class ThanksClientTest {
private lateinit var commonsApplication: CommonsApplication

private lateinit var thanksClient: ThanksClient
private lateinit var mockedApplication: MockedStatic<CommonsApplication>

/**
* initial setup, test environment
*/
@Before
@Throws(Exception::class)
fun setUp() {
MockitoAnnotations.initMocks(this)
PowerMockito.mockStatic(CommonsApplication::class.java)
PowerMockito.`when`(CommonsApplication.getInstance()).thenReturn(commonsApplication)
MockitoAnnotations.openMocks(this)
mockedApplication = Mockito.mockStatic(CommonsApplication::class.java)
`when`(CommonsApplication.getInstance()).thenReturn(commonsApplication)
thanksClient = ThanksClient(csrfTokenClient, service)
}

Expand All @@ -46,8 +50,8 @@ class ThanksClientTest {
*/
@Test
fun testThanks() {
Mockito.`when`(csrfTokenClient.tokenBlocking).thenReturn("test")
Mockito.`when`(commonsApplication.userAgent).thenReturn("test")
`when`(csrfTokenClient.tokenBlocking).thenReturn("test")
`when`(commonsApplication.userAgent).thenReturn("test")
thanksClient.thank(1L)
verify(service).thank(ArgumentMatchers.anyString(), ArgumentMatchers.any(), eq("test"), eq("test"))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class LoginActivityUnitTests {

@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
MockitoAnnotations.openMocks(this)
AppAdapter.set(TestAppAdapter())
activity = Robolectric.buildActivity(LoginActivity::class.java).create().get()
context = ApplicationProvider.getApplicationContext()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ import javax.inject.Named
class LogoutClientTest {

@Mock @field:[Inject Named("commons-service")]
internal var service: Service? = null
lateinit var service: Service

@InjectMocks
var logoutClient: LogoutClient? = null

@Before
@Throws(Exception::class)
fun setUp() {
MockitoAnnotations.initMocks(this)
MockitoAnnotations.openMocks(this)
val mwQueryResponse = mock(MwQueryResponse::class.java)
val mwQueryResult = mock(MwQueryResult::class.java)
`when`(mwQueryResult!!.csrfToken()).thenReturn("test_token")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class SessionManagerUnitTests {

@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
MockitoAnnotations.openMocks(this)
accountManager = AccountManager.get(ApplicationProvider.getApplicationContext())
shadowOf(accountManager).addAccount(account)
sessionManager =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class SignupActivityTest {

@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
MockitoAnnotations.openMocks(this)
activity = Robolectric.buildActivity(SignupActivity::class.java).create().get()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class WikiAccountAuthenticatorServiceUnitTest {

@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
MockitoAnnotations.openMocks(this)
service = WikiAccountAuthenticatorService()
service.onBind(null)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class WikiAccountAuthenticatorUnitTest {

@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
MockitoAnnotations.openMocks(this)
context = ApplicationProvider.getApplicationContext()
authenticator = WikiAccountAuthenticator(context)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class BookmarkListRootFragmentUnitTest {

@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
MockitoAnnotations.openMocks(this)
AppAdapter.set(TestAppAdapter())
activity = Robolectric.buildActivity(MainActivity::class.java).create().get()
context = ApplicationProvider.getApplicationContext()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class BookmarksPagerAdapterTests {

@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
MockitoAnnotations.openMocks(this)
bookmarksPagerAdapter = BookmarksPagerAdapter(fragmentManager, context, false)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class LoggedOutBookmarksPagerAdapterTests {
*/
@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
MockitoAnnotations.openMocks(this)
bookmarksPagerAdapter = BookmarksPagerAdapter(fragmentManager, context, true)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class BookmarkItemsControllerTest {

@Before
fun setup() {
MockitoAnnotations.initMocks(this)
MockitoAnnotations.openMocks(this)
whenever(bookmarkDao!!.allBookmarksItems)
.thenReturn(mockBookmarkList)
}
Expand Down
Loading

0 comments on commit c9dfc03

Please sign in to comment.