Skip to content

Commit

Permalink
build: add mockk
Browse files Browse the repository at this point in the history
test: created test for getCommentToUpload

fix: upload comment
  • Loading branch information
VaiTon committed Jul 28, 2022
1 parent ed68852 commit b394bef
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 15 deletions.
Expand Up @@ -15,6 +15,7 @@ import openfoodfacts.github.scrachx.openfood.AppFlavors.OBF
import openfoodfacts.github.scrachx.openfood.AppFlavors.OFF
import openfoodfacts.github.scrachx.openfood.AppFlavors.OPF
import openfoodfacts.github.scrachx.openfood.AppFlavors.OPFF
import openfoodfacts.github.scrachx.openfood.AppFlavors.isFlavors
import openfoodfacts.github.scrachx.openfood.BuildConfig
import openfoodfacts.github.scrachx.openfood.analytics.SentryAnalytics
import openfoodfacts.github.scrachx.openfood.images.*
Expand Down Expand Up @@ -387,15 +388,7 @@ class ProductRepository @Inject constructor(
username: String?,
): String = buildString {
append("Official ")
append(
when (BuildConfig.FLAVOR) {
OBF -> "Open Beauty Facts"
OPFF -> "Open Pet Food Facts"
OPF -> "Open Products Facts"
OFF -> "Open Food Facts"
else -> "Open Food Facts (unknown flavor)"
}
)
append(BuildConfig.APP_NAME)
append(" Android app ")
append(context.getVersionName())
if (username.isNullOrEmpty()) {
Expand Down
@@ -0,0 +1,55 @@
package openfoodfacts.github.scrachx.openfood.repositories

import android.content.Context
import com.google.common.truth.Truth.assertThat
import io.mockk.every
import io.mockk.junit5.MockKExtension
import io.mockk.mockk
import io.mockk.mockkStatic
import openfoodfacts.github.scrachx.openfood.BuildConfig
import openfoodfacts.github.scrachx.openfood.utils.InstallationService
import openfoodfacts.github.scrachx.openfood.utils.getVersionName
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith

@ExtendWith(MockKExtension::class)
class ProductRepositoryTest {

private lateinit var mockContext: Context
private lateinit var mockInstallationService: InstallationService

@BeforeEach
fun beforeAll() {
mockkStatic("openfoodfacts.github.scrachx.openfood.utils.ContextKt")
mockInstallationService = mockk {
every { id } returns "FAKE_ID"
}
mockContext = mockk {
every { getVersionName() } returns "TEST_VERSION_NAME"
}
}

@Test
fun `comment to upload with username`() {
// Logged in test
assertThat(
ProductRepository.getCommentToUpload(
mockContext,
mockInstallationService,
"USERNAME"
)
).isEqualTo("Official " + BuildConfig.APP_NAME + " Android app TEST_VERSION_NAME")
}

@Test
fun `comment to upload without username`() {
assertThat(
ProductRepository.getCommentToUpload(
mockContext,
mockInstallationService,
null
)
).isEqualTo("Official " + BuildConfig.APP_NAME + " Android app TEST_VERSION_NAME (Added by FAKE_ID)")
}
}
Expand Up @@ -5,6 +5,8 @@ import android.content.SharedPreferences
import android.content.res.Configuration
import android.content.res.Resources
import com.google.common.truth.Truth.assertThat
import io.mockk.every
import io.mockk.mockk
import openfoodfacts.github.scrachx.openfood.models.LanguageData
import org.junit.jupiter.api.Test
import org.mockito.kotlin.doReturn
Expand All @@ -26,14 +28,15 @@ class LocaleUtilsTest {
@Test
fun getLocale_FromContext() {
val locale = LocaleUtils.parseLocale("en-US")
val configuration = mock<Configuration> { }.apply { this.locale = locale }
val resources = mock<Resources> {
on { this.configuration } doReturn configuration
val configuration = Configuration().apply { this.locale = locale }
val resources = mockk<Resources> {
every { this@mockk.configuration } returns configuration
}
val context = mock<Context> {
on { this.resources } doReturn resources
val context = mockk<Context> {
every { this@mockk.resources } returns resources
every { getString(any()) } returns ""
}
val sharedPreferences = mock<SharedPreferences> {}
val sharedPreferences = mockk<SharedPreferences>(relaxed = true)
val localeManager = LocaleManager(context, sharedPreferences)

assertThat(localeManager.getLocaleFromContext(context)).isEqualTo(locale)
Expand Down
5 changes: 5 additions & 0 deletions gradle/libs.versions.toml
Expand Up @@ -20,6 +20,7 @@ eventbus = "3.3.1"
junit5 = "5.8.2"
androidx-hilt = "1.0.0"
org-mockito = "4.6.1"
mockk = "1.12.5"
com-squareup-retrofit2 = "2.6.4"
androidx-test-espresso = "3.4.0"

Expand Down Expand Up @@ -135,6 +136,8 @@ robolectric = "org.robolectric:robolectric:4.8.1"
mockito-core = { module = "org.mockito:mockito-core", version.ref = "org-mockito" }
mockito-kotlin = "org.mockito.kotlin:mockito-kotlin:4.0.0"
mockito-junit-jupiter = { module = "org.mockito:mockito-junit-jupiter", version.ref = "org-mockito" }
mockk-core = { module = "io.mockk:mockk", version.ref = "mockk" }
mockk-agent = { module = "io.mockk:mockk-agent-jvm", version.ref = "mockk" }
json-unit-fluent = "net.javacrumbs.json-unit:json-unit-fluent:2.35.0"
truth = "com.google.truth:truth:1.1.3"
truth-java8 = "com.google.truth.extensions:truth-java8-extension:1.1.3"
Expand Down Expand Up @@ -228,6 +231,8 @@ testing = [
"mockito-core",
"mockito-junit-jupiter",
"mockito-kotlin",
"mockk-core",
"mockk-agent",
"robolectric",
"test-kotlin-coroutines",
"truth",
Expand Down

0 comments on commit b394bef

Please sign in to comment.