Skip to content

Commit

Permalink
Replace jUnit with kotlin.test where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
MGaetan89 committed Jan 30, 2024
1 parent 31bcaef commit 685dc9d
Show file tree
Hide file tree
Showing 14 changed files with 121 additions and 108 deletions.
3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,11 @@ dependencyAnalysis {
}
}

// Required because of https://github.com/autonomousapps/dependency-analysis-gradle-plugin/issues/892
structure {
// https://github.com/autonomousapps/dependency-analysis-gradle-plugin/wiki/Customizing-plugin-behavior
ignoreKtx(true) // default is false

// Required because of https://github.com/autonomousapps/dependency-analysis-gradle-plugin/issues/892
bundle("kotlin-test") {
includeDependency(libs.kotlin.test)
}
Expand Down
5 changes: 3 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ org.gradle.parallel=true
# https://developer.android.com/topic/libraries/support-library/androidx-rn
android.useAndroidX=true

# https://github.com/autonomousapps/dependency-analysis-gradle-plugin/issues/1079#issuecomment-1862266603
dependency.analysis.test.analysis=false

# Kotlin code style for this project: "official" or "obsolete":
kotlin.code.style=official

org.gradle.caching=true
org.gradle.configuration-cache=true
# https://github.com/autonomousapps/dependency-analysis-gradle-plugin/issues/1079#issuecomment-1862266603
dependency.analysis.test.analysis=false
7 changes: 3 additions & 4 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ androidx-media = "1.7.0"
androidx-media3 = "1.2.1"
androidx-navigation = "2.7.6"
androidx-paging = "3.2.1"
androidx-test-core = "1.5.0"
androidx-test-ext-junit = "1.1.5"
androidx-test-monitor = "1.6.1"
androidx-test-runner = "1.5.2"
androidx-tv = "1.0.0-alpha10"
coil = "2.5.0"
comscore = "6.10.0"
core = "1.5.0"
dependency-analysis-gradle-plugin = "1.29.0"
detekt = "1.23.4"
guava = "31.1-android"
Expand All @@ -37,7 +37,6 @@ srg-data-provider = "0.8.0"
tag-commander-core = "5.4.2"
tag-commander-server-side = "5.5.2"
turbine = "1.0.0"
junit-ktx = "1.1.5"

[libraries]
accompanist-navigation-material = { module = "com.google.accompanist:accompanist-navigation-material", version.ref = "accompanist" }
Expand All @@ -59,12 +58,12 @@ androidx-navigation-runtime = { module = "androidx.navigation:navigation-runtime
androidx-navigation-compose = { module = "androidx.navigation:navigation-compose", version.ref = "androidx-navigation" }
androidx-paging-common = { module = "androidx.paging:paging-common", version.ref = "androidx-paging" }
androidx-paging-compose = { module = "androidx.paging:paging-compose", version.ref = "androidx-paging" }
androidx-test-core = { module = "androidx.test:core-ktx", version.ref = "androidx-test-core" }
androidx-test-monitor = { module = "androidx.test:monitor", version.ref = "androidx-test-monitor" }
androidx-test-runner = { module = "androidx.test:runner", version.ref = "androidx-test-runner" }
androidx-tv-foundation = { module = "androidx.tv:tv-foundation", version.ref = "androidx-tv" }
androidx-tv-material = { module = "androidx.tv:tv-material", version.ref = "androidx-tv" }
coil = { group = "io.coil-kt", name = "coil-compose", version.ref = "coil" }
core = { module = "androidx.test:core-ktx", version.ref = "core" }
json = { module = "org.json:json", version.ref = "json" }
kotlin-stdlib = { module = "org.jetbrains.kotlin:kotlin-stdlib", version.ref = "kotlin" }
kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
Expand Down Expand Up @@ -126,9 +125,9 @@ androidx-compose-runtime = { module = "androidx.compose.runtime:runtime" }
androidx-compose-runtime-saveable = { module = "androidx.compose.runtime:runtime-saveable" }
leanback = { group = "androidx.leanback", name = "leanback", version.ref = "androidx-leanback" }
androidx-test-ext-junit = { group = "androidx.test.ext", name = "junit", version.ref = "androidx-test-ext-junit" }
androidx-test-ext-junit-ktx = { group = "androidx.test.ext", name = "junit-ktx", version.ref = "androidx-test-ext-junit" }
guava = { module = "com.google.guava:guava", version.ref = "guava" }
turbine = { module = "app.cash.turbine:turbine", version.ref = "turbine" }
androidx-junit-ktx = { group = "androidx.test.ext", name = "junit-ktx", version.ref = "junit-ktx" }

[plugins]
android-application = { id = "com.android.application", version.ref = "android-gradle-plugin" }
Expand Down
6 changes: 3 additions & 3 deletions pillarbox-analytics/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,16 @@ dependencies {
implementation(libs.tagcommander.core)
api(libs.tagcommander.serverside)

testImplementation(libs.androidx.junit.ktx)
testImplementation(libs.core)
testImplementation(libs.robolectric)
testImplementation(libs.androidx.test.core)
testImplementation(libs.androidx.test.ext.junit.ktx)
testImplementation(libs.json) {
because("The 'org.json' package is included in the Android SDK. Adding this dependency allows us to not mock the Android SDK in unit tests.")
}
testImplementation(libs.junit)
testImplementation(libs.kotlin.test)
testImplementation(libs.mockk)
testImplementation(libs.mockk.dsl)
testImplementation(libs.robolectric)
}

kover {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,12 @@ internal class CommandersActSrg(
}

/*
* From issue :
* - https://github.com/SRGSSR/srgletterbox-android/issues/522
* - https://github.com/CommandersAct/iOSV5/issues/13
*
* And after discussion with CommandersAct teams and SRG ADI team.
*/
* From issue :
* - https://github.com/SRGSSR/srgletterbox-android/issues/522
* - https://github.com/CommandersAct/iOSV5/issues/13
*
* And after discussion with CommandersAct teams and SRG ADI team.
*/
private fun workaroundUniqueIdV4Tov5() {
// 1. Use the TC unique id value for new `device.sdk_id` property.
useLegacyUniqueIDForSdkID()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import com.comscore.Analytics
import io.mockk.mockkStatic
import io.mockk.unmockkAll
import org.junit.Test
import org.junit.runner.RunWith
import kotlin.test.AfterTest
import kotlin.test.BeforeTest
import kotlin.test.Test

@RunWith(AndroidJUnit4::class)
class SRGAnalyticsSingletonTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ import com.tagcommander.lib.serverside.schemas.TCDevice
import io.mockk.mockk
import io.mockk.slot
import io.mockk.verify
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.annotation.Config
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull
import kotlin.test.assertTrue

@RunWith(AndroidJUnit4::class)
class CommandersActSrgTest {
Expand All @@ -32,32 +34,32 @@ class CommandersActSrgTest {
@Config(qualifiers = "television")
fun `navigation device is tvbox`() {
val actual = commandersAct.getPermanentDataLabel("navigation_device")
Assert.assertEquals("tvbox", actual)
assertEquals("tvbox", actual)
}

@Test
@Config
fun `navigation device is phone`() {
val actual = commandersAct.getPermanentDataLabel("navigation_device")
Assert.assertEquals("phone", actual)
assertEquals("phone", actual)
}

@Test
@Config(qualifiers = "sw600dp")
fun `test navigation device is tablet`() {
val actual = commandersAct.getPermanentDataLabel("navigation_device")
Assert.assertEquals("tablet", actual)
assertEquals("tablet", actual)
}

@Test
@Config(qualifiers = "car")
fun `navigation device is auto`() {
val actual = commandersAct.getPermanentDataLabel("navigation_device")
Assert.assertEquals("auto", actual)
assertEquals("auto", actual)
}

@Test
fun `sendEvent() with CommandersActEvent`() {
fun `sendEvent() with CommandersActEvent`() {
val serverSide = mockk<TCServerSide>(relaxed = true)
val commandersAct = CommandersActSrg(tcServerSide = serverSide, config = analyticsConfig, "tests")
val eventSlot = slot<TCEvent>()
Expand All @@ -67,8 +69,8 @@ class CommandersActSrgTest {
serverSide.execute(capture(eventSlot))
}

Assert.assertTrue(eventSlot.isCaptured)
Assert.assertEquals(eventSlot.captured.name, "dummy")
assertTrue(eventSlot.isCaptured)
assertEquals(eventSlot.captured.name, "dummy")
}

@Test
Expand All @@ -88,18 +90,18 @@ class CommandersActSrgTest {
verify(exactly = 1) {
serverSide.execute(capture(eventSlot))
}
Assert.assertTrue(eventSlot.isCaptured)
assertTrue(eventSlot.isCaptured)
val capturedEvent = eventSlot.captured
Assert.assertEquals("page_view", capturedEvent.name)
Assert.assertEquals("PageTitle1", capturedEvent.pageName)
Assert.assertEquals("UnitTest", capturedEvent.pageType)
Assert.assertEquals("pillarbox", capturedEvent.additionalProperties["navigation_level_1"])
Assert.assertEquals("unit-test", capturedEvent.additionalProperties["navigation_level_2"])
Assert.assertNull(capturedEvent.additionalProperties["navigation_level_3"])
assertEquals("page_view", capturedEvent.name)
assertEquals("PageTitle1", capturedEvent.pageName)
assertEquals("UnitTest", capturedEvent.pageType)
assertEquals("pillarbox", capturedEvent.additionalProperties["navigation_level_1"])
assertEquals("unit-test", capturedEvent.additionalProperties["navigation_level_2"])
assertNull(capturedEvent.additionalProperties["navigation_level_3"])
}

@Test
fun `sendTcMediaEvent() with TCMediaEvent`() {
fun `sendTcMediaEvent() with TCMediaEvent`() {
val serverSide = mockk<TCServerSide>(relaxed = true)
val commandersAct = CommandersActSrg(tcServerSide = serverSide, config = analyticsConfig, "tests")
val eventSlot = slot<TCEvent>()
Expand All @@ -109,27 +111,27 @@ class CommandersActSrgTest {
serverSide.execute(capture(eventSlot))
}

Assert.assertTrue(eventSlot.isCaptured)
Assert.assertEquals("eof", eventSlot.captured.name)
assertTrue(eventSlot.isCaptured)
assertEquals("eof", eventSlot.captured.name)
}

@Test
fun `initial consent services`() {
Assert.assertNull(commandersAct.getPermanentDataLabel(CommandersActLabels.CONSENT_SERVICES.label))
assertNull(commandersAct.getPermanentDataLabel(CommandersActLabels.CONSENT_SERVICES.label))
}

@Test
fun `set consent services`() {
val services = listOf("service1", "service2")
val expected = "service1,service2"
commandersAct.setConsentServices(services)
Assert.assertEquals(expected, commandersAct.getPermanentDataLabel(CommandersActLabels.CONSENT_SERVICES.label))
assertEquals(expected, commandersAct.getPermanentDataLabel(CommandersActLabels.CONSENT_SERVICES.label))
}

@Test
fun `legacyUniqueID is used for sdkID and anonymous_id`() {
val legacyUniqueId: String = TCPredefinedVariables.getInstance().uniqueIdentifier
Assert.assertEquals(legacyUniqueId, TCDevice.getInstance().sdkID)
Assert.assertEquals(legacyUniqueId, TCUser.getInstance().anonymous_id)
assertEquals(legacyUniqueId, TCDevice.getInstance().sdkID)
assertEquals(legacyUniqueId, TCUser.getInstance().anonymous_id)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import io.mockk.mockk
import io.mockk.mockkStatic
import io.mockk.unmockkAll
import io.mockk.verify
import org.junit.Test
import org.junit.runner.RunWith
import kotlin.test.AfterTest
import kotlin.test.BeforeTest
import kotlin.test.Test

@RunWith(AndroidJUnit4::class)
class ComScoreSrgTest {
Expand Down
6 changes: 3 additions & 3 deletions pillarbox-core-business/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ dependencies {
implementation(libs.okhttp.logging.interceptor)
api(libs.tagcommander.core)

testImplementation(libs.androidx.junit.ktx)
testImplementation(libs.core)
testImplementation(libs.robolectric)
testImplementation(libs.androidx.test.core)
testImplementation(libs.androidx.test.ext.junit.ktx)
testImplementation(libs.junit)
testImplementation(libs.kotlin.test)
testImplementation(libs.kotlinx.coroutines.test)
testImplementation(libs.ktor.client.mock)
testImplementation(libs.mockk)
testImplementation(libs.mockk.dsl)
testImplementation(libs.robolectric)

androidTestImplementation(project(":pillarbox-player-testutils"))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ package ch.srgssr.pillarbox.core.business

import ch.srgssr.pillarbox.core.business.MediaCompositionMediaItemSource.ImageScalingService
import ch.srgssr.pillarbox.core.business.integrationlayer.service.IlHost
import org.junit.Assert.assertEquals
import org.junit.Test
import java.net.URLEncoder
import kotlin.test.Test
import kotlin.test.assertEquals

class ImageScalingServiceTest {
@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ import ch.srgssr.pillarbox.core.business.integrationlayer.data.Resource
import ch.srgssr.pillarbox.core.business.integrationlayer.data.Segment
import ch.srgssr.pillarbox.core.business.integrationlayer.service.MediaCompositionDataSource
import kotlinx.coroutines.runBlocking
import org.junit.Assert
import org.junit.Test
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertTrue

class MediaCompositionMediaItemSourceTest {

Expand Down Expand Up @@ -51,20 +53,21 @@ class MediaCompositionMediaItemSourceTest {
@Test
fun testCompatibleResource() = runBlocking {
val mediaItem = mediaItemSource.loadMediaItem(createMediaItem(DummyMediaCompositionProvider.URN_HLS_RESOURCE))
Assert.assertNotNull(mediaItem)
assertNotNull(mediaItem)
Unit
}

@Test
fun testMetadata() = runBlocking {
val mediaItem = mediaItemSource.loadMediaItem(createMediaItem(DummyMediaCompositionProvider.URN_METADATA))
Assert.assertNotNull(mediaItem)
assertNotNull(mediaItem)
val metadata = mediaItem.mediaMetadata
val expected = MediaMetadata.Builder()
.setTitle("Title")
.setSubtitle("Lead")
.setDescription("Description")
.build()
Assert.assertEquals(expected, metadata)
assertEquals(expected, metadata)
}

@Test
Expand All @@ -75,10 +78,10 @@ class MediaCompositionMediaItemSourceTest {
.setDescription("CustomDescription")
.build()
val mediaItem = mediaItemSource.loadMediaItem(createMediaItem(DummyMediaCompositionProvider.URN_METADATA, input))
Assert.assertNotNull(mediaItem)
assertNotNull(mediaItem)
val metadata = mediaItem.mediaMetadata
val expected = input.buildUpon().build()
Assert.assertEquals(expected, metadata)
assertEquals(expected, metadata)
}

@Test
Expand All @@ -87,28 +90,28 @@ class MediaCompositionMediaItemSourceTest {
.setTitle("CustomTitle")
.build()
val mediaItem = mediaItemSource.loadMediaItem(createMediaItem(DummyMediaCompositionProvider.URN_METADATA, input))
Assert.assertNotNull(mediaItem)
assertNotNull(mediaItem)
val metadata = mediaItem.mediaMetadata
val expected = MediaMetadata.Builder()
.setTitle("CustomTitle")
.setSubtitle("Lead")
.setDescription("Description")
.build()
Assert.assertEquals(expected, metadata)
assertEquals(expected, metadata)
}

@Test(expected = BlockReasonException::class)
fun testBlockReason() = runBlocking {
val input = MediaMetadata.Builder().build()
mediaItemSource.loadMediaItem(createMediaItem(DummyMediaCompositionProvider.URN_BLOCK_REASON, input))
Assert.assertTrue(false)
assertTrue(false)
}

@Test(expected = BlockReasonException::class)
fun testBlockedSegment() = runBlocking {
val input = MediaMetadata.Builder().build()
mediaItemSource.loadMediaItem(createMediaItem(DummyMediaCompositionProvider.URN_SEGMENT_BLOCK_REASON, input))
Assert.assertTrue(false)
assertTrue(false)
}

internal class DummyMediaCompositionProvider : MediaCompositionDataSource {
Expand Down
Loading

0 comments on commit 685dc9d

Please sign in to comment.