Skip to content

Commit

Permalink
test: Convert from Mockito to Mockk and Assertj to assertk
Browse files Browse the repository at this point in the history
  • Loading branch information
kingargyle committed Dec 16, 2024
1 parent 2aef511 commit 1ef2161
Show file tree
Hide file tree
Showing 10 changed files with 641 additions and 607 deletions.
3 changes: 2 additions & 1 deletion emby-lib/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ dependencies {
debugImplementation(libs.toothpick.smoothie)

releaseApi(libs.toothpick.javax.annotations)
// kapt(libs.toothpick.compiler)
ksp(libs.toothpick.ksp.compiler)

implementation(libs.android.priority.jobqueue)
Expand All @@ -81,7 +80,9 @@ dependencies {

testImplementation(libs.junit)
testImplementation(libs.assertj.core)
testImplementation(libs.assertk.jvm)
testImplementation(libs.mockito.core)
testImplementation(libs.mockk)
testImplementation(libs.robolectric)
testImplementation(libs.robolectric.shadows.framework)
testImplementation(libs.robolectric.shadows.api)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import kotlinx.coroutines.test.runTest
import org.junit.Assert.*
import org.junit.Before
import org.junit.Test
import org.robolectric.shadows.ShadowLog

class EmbyServerDiscoverTest {

private lateinit var serverDiscovery : EmbyServerDiscover

@Before
fun setUp() {
ShadowLog.stream = System.out
serverDiscovery = EmbyServerDiscover()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package us.nineworlds.serenity.emby.server

import android.content.Context
import io.mockk.mockk
import org.greenrobot.eventbus.EventBus
import org.junit.Before
import org.junit.Test
Expand All @@ -10,6 +11,7 @@ import org.mockito.MockitoAnnotations.initMocks
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
import org.robolectric.shadows.ShadowLog
import toothpick.config.Module
import us.nineworlds.serenity.common.android.injection.ApplicationContext
import us.nineworlds.serenity.emby.test.InjectingTest
Expand All @@ -18,17 +20,15 @@ import us.nineworlds.serenity.emby.test.InjectingTest
@Config(sdk = [28])
class EmbyServerJobTest : InjectingTest() {

@Mock
lateinit var mockEventBus: EventBus

override val modules: List<Any>
get() = mutableListOf(TestModule())

lateinit var job: EmbyServerJob
private lateinit var job: EmbyServerJob

@Before
override fun setUp() {
initMocks(this)
ShadowLog.stream = System.out

super.setUp()
job = EmbyServerJob()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package us.nineworlds.serenity.emby.server.api

import androidx.test.core.app.ApplicationProvider
import org.assertj.core.api.Assertions.assertThat
import assertk.assertThat
import assertk.assertions.hasSize
import assertk.assertions.isNotEmpty
import assertk.assertions.isNotNull
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
Expand All @@ -15,37 +18,37 @@ import us.nineworlds.serenity.emby.server.model.AuthenticationResult
@Config(sdk = [28])
class EmbyAPIClientTest {

lateinit var client: EmbyAPIClient
private lateinit var client: EmbyAPIClient

@Before
fun setUp() {
ShadowLog.stream = System.out

client = EmbyAPIClient(context = ApplicationProvider.getApplicationContext())
client.updateBaseUrl("http://192.168.86.162:8096")
client.updateBaseUrl("http://192.168.68.95:8096")
}

@Test
fun retrieveAllPublicUsers() {
val result = client.fetchAllPublicUsers()
assertThat(result).isNotEmpty.hasSize(2)
assertThat(result).hasSize(2)
}

@Test fun loginAdminUser() {
val authenticateResult = authenticate()

assertThat(authenticateResult).isNotNull()
assertThat(authenticateResult.accesToken).isNotBlank()
assertThat(client.serverId).isNotBlank()
assertThat(client.accessToken).isNotBlank()
assertThat(client.userId).isNotBlank()
assertThat(authenticateResult.accesToken).isNotEmpty()
assertThat(client.serverId).isNotEmpty()
assertThat(client.accessToken).isNotNull()
assertThat(client.userId).isNotNull().isNotEmpty()
}

@Test fun testCurrentUsersViews() {
authenticate()

val result = client.currentUserViews()
assertThat(result.items).isNotEmpty
assertThat(result.items).isNotEmpty()
}

@Test fun availableFiltersForCurrentUser() {
Expand All @@ -61,8 +64,8 @@ class EmbyAPIClientTest {
authenticate()

val result = client.retrieveRootData()
assertThat(result).isNotNull
assertThat(result!!.directories).isNotEmpty
assertThat(result).isNotNull()
assertThat(result.directories).isNotEmpty()
}

@Test fun createCategoriesForParentId() {
Expand All @@ -74,7 +77,7 @@ class EmbyAPIClientTest {

val categories = client.retrieveCategoriesById(parentId)

assertThat(categories.directories).isNotEmpty
assertThat(categories.directories).isNotEmpty()
}

@Test fun fetchAllMovies() {
Expand All @@ -85,7 +88,7 @@ class EmbyAPIClientTest {

val movies = client.retrieveItemByIdCategory(parentId, "all", Types.EPISODE)

assertThat(movies.videos).isNotEmpty
assertThat(movies.videos).isNotEmpty()
}

@Test fun fetchAllLatestMovies() {
Expand All @@ -97,7 +100,7 @@ class EmbyAPIClientTest {

val itemResult = client.retrieveItemByIdCategory(key, "recentlyAdded", Types.MOVIES)

assertThat(itemResult.videos).isNotEmpty
assertThat(itemResult.videos).isNotEmpty()

}

Expand Down
6 changes: 4 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
appVersion = "3.0-M1"
annotationVersion = "1.9.1"
appcompatVersion = "1.7.0"
assertkJvmVersion = "0.24"
assertkJvmVersion = "0.28.1"
cardviewVersion = "1.0.0"
commonsIo = "2.13.0"
commonsLang3Version = "3.7"
Expand Down Expand Up @@ -55,6 +55,7 @@ easydeviceInfoVersion = "2.4.1"
androidxTestCoreVersion = "1.6.1"
androidxRecyclerViewVersion = "1.3.2"
resourcefulVersion = "1.1.0"
mockkVersion = "1.13.13"

[libraries]
androidx-annotation = { module = "androidx.annotation:annotation", version.ref = "annotationVersion" }
Expand All @@ -69,7 +70,6 @@ androidx-leanback-preference = { module = "androidx.leanback:leanback-preference
androidx-legacy-support-v4 = { module = "androidx.legacy:legacy-support-v4", version.ref = "legacySupportV4Version" }
androidx-percentlayout = { module = "androidx.percentlayout:percentlayout", version.ref = "percentlayoutVersion" }
androidx-recycler-view = { module = "androidx.recyclerview:recyclerview", version.ref = "androidxRecyclerViewVersion"}
assertk-jvm = { module = "com.willowtreeapps.assertk:assertk-jvm", version.ref = "assertkJvmVersion" }
commons-io = { module = "commons-io:commons-io", version.ref = "commonsIo" }
commons-lang3 = { module = "org.apache.commons:commons-lang3", version.ref = "commonsLang3Version" }
exoplayer-core = { module = "com.google.android.exoplayer:exoplayer-core", version.ref = "exoplayerVersion"}
Expand Down Expand Up @@ -123,6 +123,7 @@ assertj-core = { group = "org.assertj", name = "assertj-core", version.ref = "as
assertj-android = { group = "com.squareup.assertj", name = "assertj-android", version.ref = "assertJAndroidVersion"}
mockito-core = { group = "org.mockito", name = "mockito-core", version.ref = "mockitoVersion" }
mockito-kotlin = { module = "com.nhaarman.mockitokotlin2:mockito-kotlin", version = "2.2.0"}
mockk = { module = "io.mockk:mockk", version.ref = "mockkVersion" }
robolectric = { group = "org.robolectric", name = "robolectric", version.ref = "robolectricVersion" }
robolectric-shadows-framework = { group = "org.robolectric", name = "shadows-framework", version.ref = "robolectricVersion" }
robolectric-shadows-api = { group = "org.robolectric", name = "shadowapi", version.ref = "robolectricVersion" }
Expand All @@ -133,5 +134,6 @@ androidx-test-core = { group = "androidx.test", name = "core", version.ref = "an
kotlin-coroutines-test = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-test", version.ref = "kotlinCoroutinesVersion" }

toothpick-testing = { group = "com.github.stephanenicolas.toothpick", name = "toothpick-testing", version.ref = "toothPickVersion"}
assertk-jvm = { module = "com.willowtreeapps.assertk:assertk", version.ref = "assertkJvmVersion" }

[plugins]
1 change: 1 addition & 0 deletions serenity-app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ dependencies {
exclude(group = "xpp3")
}

testImplementation(libs.mockk)
testImplementation(libs.mockito.core)
testImplementation(libs.mockito.kotlin)
testImplementation(libs.commons.lang3)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package us.nineworlds.serenity

import android.content.Context
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import com.birbit.android.jobqueue.JobManager
import io.mockk.mockk
import toothpick.config.Module
import us.nineworlds.serenity.common.rest.SerenityClient
import us.nineworlds.serenity.core.logger.Logger
import us.nineworlds.serenity.core.util.AndroidHelper

class MockkTestingModule : Module() {
companion object {
val mockJobManager: JobManager = mockk(relaxed = true)
val mockPlexAppFactory: SerenityClient = mockk(relaxed = true)
val mockLocalBroadcastManager: LocalBroadcastManager = mockk(relaxed = true)
var mockLogger: Logger = mockk(relaxed = true)
var mockAndroidHelper: AndroidHelper = mockk(relaxed = true)
}

init {
bind(JobManager::class.java).toInstance(mockJobManager)
bind(SerenityClient::class.java).toInstance(mockPlexAppFactory)
bind(LocalBroadcastManager::class.java).toInstance(mockLocalBroadcastManager)
bind(Logger::class.java).toInstance(mockLogger)
bind(AndroidHelper::class.java).toInstance(mockAndroidHelper)
}
}
Loading

0 comments on commit 1ef2161

Please sign in to comment.