Skip to content

Commit

Permalink
test: Additional tests converted to mockk and assertk
Browse files Browse the repository at this point in the history
  • Loading branch information
kingargyle committed Dec 16, 2024
1 parent 1ef2161 commit 8b34e1a
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,21 @@ import android.content.SharedPreferences
import android.preference.PreferenceManager
import android.widget.TextView
import androidx.test.core.app.ApplicationProvider
import com.nhaarman.mockitokotlin2.doReturn
import com.nhaarman.mockitokotlin2.whenever
import io.mockk.clearAllMocks
import io.mockk.every
import io.mockk.mockk
import org.assertj.android.api.Assertions
import org.assertj.core.api.Assertions.assertThat
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.junit.MockitoJUnit
import org.mockito.quality.Strictness.STRICT_STUBS
import org.robolectric.Robolectric
import org.robolectric.RobolectricTestRunner
import org.robolectric.Shadows.shadowOf
import org.robolectric.annotation.LooperMode
import toothpick.config.Module
import us.nineworlds.serenity.MockkTestingModule
import us.nineworlds.serenity.R
import us.nineworlds.serenity.TestingModule
import us.nineworlds.serenity.common.Server
import us.nineworlds.serenity.core.util.StringPreference
import us.nineworlds.serenity.injection.ForMediaServers
Expand All @@ -36,26 +33,25 @@ import java.util.UUID
import java.util.concurrent.ConcurrentHashMap

@RunWith(RobolectricTestRunner::class)
@LooperMode(LooperMode.Mode.LEGACY)
class ServerSelectionActivityTest : InjectingTest() {
companion object {
private val mockServer = mockk<Server>(relaxed = true)
}

@Rule
@JvmField
val rule = MockitoJUnit.rule().strictness(STRICT_STUBS)

@Mock
lateinit var mockServer: Server

lateinit var activity: ServerSelectionActivity
private lateinit var activity: ServerSelectionActivity

@Before
override fun setUp() {
clearAllMocks()
super.setUp()
Robolectric.getBackgroundThreadScheduler().pause()
Robolectric.getForegroundThreadScheduler().pause()
activity = Robolectric.buildActivity(ServerSelectionActivity::class.java).create().visible().get()
}

@After
fun tearDown() {
activity.finish()
}

@Test
fun serverDelayHandlerIsInitialized() {
assertThat(activity.serverDisplayHandler).isNotNull
Expand All @@ -64,7 +60,6 @@ class ServerSelectionActivityTest : InjectingTest() {
@Test
fun createServerListHidesProgressBarAndDisplaysServerContainer() {
Robolectric.flushForegroundThreadScheduler()
Robolectric.flushBackgroundThreadScheduler()

Assertions.assertThat(activity.progressBinding.dataLoadingContainer).isGone
Assertions.assertThat(activity.binding.serverContainer).isVisible
Expand All @@ -75,10 +70,9 @@ class ServerSelectionActivityTest : InjectingTest() {
val expectedId = UUID.randomUUID().toString()
activity.servers[expectedId] = mockServer

doReturn("Emby").whenever(mockServer).discoveryProtocol()
doReturn("test").whenever(mockServer).serverName
every { mockServer.discoveryProtocol() } returns "Emby"
every { mockServer.serverName } returns "test"

Robolectric.flushBackgroundThreadScheduler()
Robolectric.flushForegroundThreadScheduler()

Assertions.assertThat(activity.binding.serverContainer).hasChildCount(3)
Expand All @@ -87,7 +81,6 @@ class ServerSelectionActivityTest : InjectingTest() {
@Test
fun refreshButtonIsOnlyItemWhenServerListIsEmpty() {
Robolectric.flushForegroundThreadScheduler()
Robolectric.flushForegroundThreadScheduler()

assertThat(activity.servers).isEmpty()
Assertions.assertThat(activity.binding.serverContainer).hasChildCount(2);
Expand All @@ -98,10 +91,9 @@ class ServerSelectionActivityTest : InjectingTest() {
val expectedId = UUID.randomUUID().toString()
activity.servers[expectedId] = mockServer

doReturn("Emby").whenever(mockServer).discoveryProtocol()
doReturn("test").whenever(mockServer).serverName
every { mockServer.discoveryProtocol() } returns "Emby"
every { mockServer.serverName } returns "test"

Robolectric.flushBackgroundThreadScheduler()
Robolectric.flushForegroundThreadScheduler()

val view = activity.binding.serverContainer.getChildAt(1)
Expand All @@ -115,11 +107,10 @@ class ServerSelectionActivityTest : InjectingTest() {
val expectedId = UUID.randomUUID().toString()
activity.servers[expectedId] = mockServer

doReturn("Emby").whenever(mockServer).discoveryProtocol()
doReturn("test").whenever(mockServer).serverName
doReturn("testserver").whenever(mockServer).ipAddress
every { mockServer.discoveryProtocol() } returns "Emby"
every { mockServer.serverName } returns "test"
every { mockServer.ipAddress } returns "testserver"

Robolectric.flushBackgroundThreadScheduler()
Robolectric.flushForegroundThreadScheduler()

val view = activity.binding.serverContainer.getChildAt(1)
Expand All @@ -130,7 +121,7 @@ class ServerSelectionActivityTest : InjectingTest() {
}

override fun installTestModules() {
scope.installTestModules(TestingModule(), TestModule())
scope.installTestModules(MockkTestingModule(), TestModule())
}

inner class TestModule : Module() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package us.nineworlds.serenity.ui.activity.login

import assertk.assertThat
import assertk.assertions.isEqualTo
import com.nhaarman.mockitokotlin2.any
import com.nhaarman.mockitokotlin2.doReturn
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.verify
import com.nhaarman.mockitokotlin2.whenever
import io.mockk.clearAllMocks
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.test.setMain
import org.junit.After
import org.junit.Before
Expand All @@ -19,15 +20,19 @@ import us.nineworlds.serenity.common.rest.SerenityClient
import us.nineworlds.serenity.common.rest.SerenityUser
import us.nineworlds.serenity.core.repository.LoginRepository

@OptIn(ExperimentalCoroutinesApi::class)
class LoginRepositoryTest {

private val mockSerenityClient = mock<SerenityClient>()
private val mockSerenityUser = mock<SerenityUser>()
private companion object {
private val mockSerenityClient = mockk<SerenityClient>()
private val mockSerenityUser = mockk<SerenityUser>()
}

private lateinit var repository: LoginRepository

@Before
fun setUp() {
clearAllMocks()
Dispatchers.setMain(Dispatchers.Unconfined)
repository = LoginRepository(mockSerenityClient)
}
Expand All @@ -38,29 +43,26 @@ class LoginRepositoryTest {
}

@Test
fun testLoadAllUsersWithSuccess() {
fun testLoadAllUsersWithSuccess() = runTest(UnconfinedTestDispatcher()) {
val expectedResult = listOf(mockSerenityUser)
doReturn(expectedResult).whenever(mockSerenityClient).allAvailableUsers()
every { mockSerenityClient.allAvailableUsers() } returns expectedResult

val result = runBlocking {
repository.loadAllUsers()
}
val result = repository.loadAllUsers()

assertThat((result as Result.Success<List<SerenityUser>>).data).isEqualTo(expectedResult)
verify(mockSerenityClient).allAvailableUsers()

verify { mockSerenityClient.allAvailableUsers() }
}

@Test
fun testAuthenticateUser() {
fun testAuthenticateUser() = runTest(UnconfinedTestDispatcher()) {
val expectedResult = mockSerenityUser
doReturn(expectedResult).whenever(mockSerenityClient).authenticateUser(any())
every { mockSerenityClient.authenticateUser(any()) } returns expectedResult

val result = runBlocking {
repository.authenticateUser(expectedResult)
}
val result = repository.authenticateUser(expectedResult)

assertThat((result as Result.Success<SerenityUser>).data).isEqualTo(expectedResult)
verify(mockSerenityClient).authenticateUser(mockSerenityUser)
verify { mockSerenityClient.authenticateUser(expectedResult) }
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import android.view.View
import androidx.test.core.app.ApplicationProvider
import assertk.assertThat
import assertk.assertions.isNotNull
import com.nhaarman.mockitokotlin2.spy
import com.nhaarman.mockitokotlin2.verify
import io.mockk.spyk
import io.mockk.verify
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
Expand All @@ -23,7 +23,7 @@ class ManualServerActivityTest {
@Before
fun setUp() {
ApplicationProvider.getApplicationContext<Application>().setTheme(R.style.AppTheme)
activity = spy(Robolectric.buildActivity(ManualServerActivity::class.java).create().get())
activity = spyk(Robolectric.buildActivity(ManualServerActivity::class.java).create().get())
}

@Test
Expand All @@ -34,6 +34,7 @@ class ManualServerActivityTest {
@Test
fun finishSetsResultToMainMenuPreferenceResultCode() {
activity.finish()
verify(activity).setResult(MainActivity.MAIN_MENU_PREFERENCE_RESULT_CODE)

verify { activity.setResult(MainActivity.MAIN_MENU_PREFERENCE_RESULT_CODE) }
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package us.nineworlds.serenity.ui.activity.manualentry

import androidx.test.ext.junit.runners.AndroidJUnit4
import com.nhaarman.mockitokotlin2.any
import com.nhaarman.mockitokotlin2.doNothing
import com.nhaarman.mockitokotlin2.spy
import com.nhaarman.mockitokotlin2.verify
import com.nhaarman.mockitokotlin2.whenever
import io.mockk.Runs
import io.mockk.every
import io.mockk.just
import io.mockk.spyk
import io.mockk.verify
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
Expand All @@ -23,16 +23,16 @@ class ManualServerSettingsFragmentTest {
fun setUp() {
Robolectric.getForegroundThreadScheduler().pause()

fragment = spy(ManualServerSettingsFragment())
fragment = spyk(ManualServerSettingsFragment())
FragmentTestUtil.startFragment(fragment)
}

@Test
fun startsInitialScreen() {
doNothing().whenever(fragment).startPreferenceFragment(any())
every { fragment.startPreferenceFragment(any()) } just Runs
fragment.onPreferenceStartInitialScreen()

verify(fragment).startPreferenceFragment(any<ManualServerSettingsFragment.Companion.ServerSettingsPreferenceFragment>())
verify { fragment.startPreferenceFragment(any<ManualServerSettingsFragment.Companion.ServerSettingsPreferenceFragment>()) }
}

}
Loading

0 comments on commit 8b34e1a

Please sign in to comment.