Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade Mockito 4.11.0 -> 5.5.0 #43

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
9 changes: 5 additions & 4 deletions buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ object Versions {
const val COROUTINES = "1.7.3"
const val ANDROID_GRADLE_PLUGIN = "7.4.2"
const val JUNIT = "4.13.2"
const val ROBOLECTRIC = "4.3"
const val MOCKITO = "4.11.0"
const val MOCKITO_KOTLIN = "4.1.0"
const val ROBOLECTRIC = "4.10.3"
kyrillosgait marked this conversation as resolved.
Show resolved Hide resolved
const val MOCKITO = "5.5.0"
const val MOCKITO_INLINE = "5.2.0"
const val MOCKITO_KOTLIN = "5.1.0"
const val CORE_KTX = "1.6.0"
const val FRAGMENT_KTX = "1.2.4"
const val DETEKT_RUNTIME = "1.23.1"
Expand Down Expand Up @@ -38,7 +39,7 @@ object Libs {
const val CORE_KTX = "androidx.core:core-ktx:${Versions.CORE_KTX}"
const val KOTLIN_TESTS = "org.jetbrains.kotlin:kotlin-test:${Versions.KOTLIN}"
const val FRAGMENT_KTX = "androidx.fragment:fragment-ktx:${Versions.FRAGMENT_KTX}"
const val MOCKITO_INLINE = "org.mockito:mockito-inline:${Versions.MOCKITO}"
const val MOCKITO_INLINE = "org.mockito:mockito-inline:${Versions.MOCKITO_INLINE}"
const val LIFECYCLE = "androidx.lifecycle:lifecycle-common-java8:${Versions.LIFECYCLE}"
@Suppress("MaxLineLength")
const val VIEW_BINDING_DELEGATE = "com.github.kirich1409:viewbindingpropertydelegate-noreflection:${Versions.VIEW_BINDING_DELEGATE}"
Expand Down
9 changes: 9 additions & 0 deletions library/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ group = "com.github.vinted"
android {
compileSdk = Versions.COMPILE_SDK_VERSION

compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}

kotlinOptions {
jvmTarget = "11"
}

defaultConfig {
minSdk = Versions.MIN_SDK_VERSION
}
Expand Down
81 changes: 46 additions & 35 deletions library/src/test/java/com/vinted/coper/CoperImplTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito.*
import org.mockito.internal.util.MockUtil
import org.mockito.kotlin.anyArray
import org.mockito.kotlin.anyOrNull
import org.mockito.kotlin.whenever
Expand Down Expand Up @@ -430,11 +431,7 @@ class CoperImplTest {
val crashPermission = "crash"
mockCheckPermissions(crashPermission, PackageManager.PERMISSION_DENIED)


executePermissionRequest(
permissionsToRequest = emptyList(),
permissionResult = listOf(PermissionChecker.PERMISSION_GRANTED)
)
fixture.request(*emptyList<String>().toTypedArray())
}

@Test(expected = IllegalStateException::class)
Expand Down Expand Up @@ -518,19 +515,6 @@ class CoperImplTest {
fun request_permissionResultCameWithDifferentPermissions_jobIsNotCompleted() = runTest {
val permission = "permission"
mockCheckPermissions(permission, PackageManager.PERMISSION_DENIED)
val fragment = fixture.getFragmentSafely()
whenever(
fragment.requestPermissions(
eq(listOf(permission).toTypedArray()),
anyOrNull()
)
).then {
fragment.onRequestPermissionResult(
permissions = listOf("test"),
permissionsResult = listOf(PermissionChecker.PERMISSION_GRANTED),
requestCode = CoperFragment.REQUEST_CODE
)
}

val responseAsync = async {
fixture.request(permission)
Expand Down Expand Up @@ -560,17 +544,18 @@ class CoperImplTest {
fun request_twoIdenticalRequest_twoRequestCompleted() = runTest {
val permission = "sameRequest"
mockCheckPermissions(permission, PermissionChecker.PERMISSION_DENIED)

stubRequestPermission(
coperFragment = fixture.getFragmentSafely(),
permissions = listOf(permission),
permissionResults = listOf(PermissionChecker.PERMISSION_GRANTED),
)

val responseAsync1 = async {
executePermissionRequest(
permissionsToRequest = listOf(permission),
permissionResult = listOf(PermissionChecker.PERMISSION_GRANTED)
)
fixture.request(*listOf(permission).toTypedArray())
}
val responseAsync2 = async {
executePermissionRequest(
permissionsToRequest = listOf(permission),
permissionResult = listOf(PermissionChecker.PERMISSION_GRANTED)
)
fixture.request(*listOf(permission).toTypedArray())
}
val result2 = responseAsync2.await()
val result1 = responseAsync1.await()
Expand Down Expand Up @@ -615,8 +600,8 @@ class CoperImplTest {
val coperFragment = fixture.getFragmentSafely()
whenever(
coperFragment.requestPermissions(
eq(arrayOf(firstPermission, secondPermission)),
anyOrNull()
arrayOf(firstPermission, secondPermission),
CoperFragment.REQUEST_CODE
)
).then {
coperFragment.onRequestPermissionResult(
Expand Down Expand Up @@ -760,11 +745,35 @@ class CoperImplTest {
fun request_requestCodeIsNotOfCoperFragment_throwException() = runTest {
val requestedPermission = "requested_permission"

executePermissionRequest(
permissionsToRequest = listOf(requestedPermission),
permissionResult = listOf(PermissionChecker.PERMISSION_DENIED),
requestCode = 0
)
val coperFragment = fixture.getFragmentSafely()

whenever(
coperFragment.requestPermissions(
listOf(requestedPermission).toTypedArray(),
0,
)
).then {
coperFragment.onRequestPermissionResult(
permissions = listOf(requestedPermission),
permissionsResult = listOf(PermissionChecker.PERMISSION_DENIED),
requestCode = 0,
)
}

whenever(
coperFragment.requestPermissions(
listOf(requestedPermission).toTypedArray(),
CoperFragment.REQUEST_CODE
)
).then {
coperFragment.onRequestPermissionResult(
permissions = listOf(requestedPermission),
permissionsResult = listOf(PermissionChecker.PERMISSION_DENIED),
requestCode = 0,
)
}

fixture.request(*listOf(requestedPermission).toTypedArray())
}

private suspend fun executePermissionRequest(
Expand Down Expand Up @@ -813,8 +822,8 @@ class CoperImplTest {
) {
whenever(
coperFragment.requestPermissions(
eq(permissions.toTypedArray()),
anyOrNull()
permissions.toTypedArray(),
requestCode,
)
).then {
coperFragment.onRequestPermissionResult(
Expand All @@ -838,6 +847,8 @@ class CoperImplTest {

private suspend fun CoperImpl.mockGetFragmentWithStub() {
val coperFragment = getFragmentSafely()
if (MockUtil.isMock(coperFragment)) return

val spyCoperFragment = spy(coperFragment)
// This is needed because spy creates new instance and stubbing needs exact reference
coperFragment.requireActivity().supportFragmentManager.beginTransaction()
Expand Down