Skip to content

Commit

Permalink
Update tests infrastructre and dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
st235 committed Aug 18, 2023
1 parent 660a854 commit b0d03d2
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 59 deletions.
5 changes: 2 additions & 3 deletions lib-expandablebottombar/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,10 @@ dependencies {
implementation 'androidx.navigation:navigation-fragment-ktx:2.7.0'

testImplementation 'junit:junit:4.13.2'
testImplementation 'org.mockito:mockito-core:4.6.1'
testImplementation 'org.mockito:mockito-inline:4.6.1'
testImplementation 'org.mockito:mockito-core:5.3.1'
testImplementation 'org.hamcrest:hamcrest-library:2.2'
testImplementation 'androidx.test:core:1.5.0'
testImplementation 'com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0'
testImplementation 'org.mockito.kotlin:mockito-kotlin:5.1.0'
testImplementation 'org.robolectric:robolectric:4.8.1'

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package github.com.st235.expandablebottombar

import android.content.Context
import com.nhaarman.mockitokotlin2.mock
import github.com.st235.lib_expandablebottombar.MenuItemDescriptor
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.mockito.kotlin.mock

@RunWith(JUnit4::class)
class MenuItemDescriptorTest {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
package github.com.st235.expandablebottombar

import android.content.Context
import android.content.res.ColorStateList
import android.graphics.drawable.ColorStateListDrawable
import android.view.View
import androidx.test.core.app.ApplicationProvider
import com.nhaarman.mockitokotlin2.anyOrNull
import github.com.st235.lib_expandablebottombar.test.R
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.whenever
import github.com.st235.lib_expandablebottombar.ExpandableBottomBar
import github.com.st235.lib_expandablebottombar.MenuItem
import github.com.st235.lib_expandablebottombar.MenuItemDescriptor
import github.com.st235.lib_expandablebottombar.MenuItemFactory
import github.com.st235.lib_expandablebottombar.components.MenuItemView
import github.com.st235.lib_expandablebottombar.utils.DrawableHelper
import github.com.st235.lib_expandablebottombar.test.R
import github.com.st235.lib_expandablebottombar.utils.StyleController
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito.*
import org.mockito.kotlin.anyOrNull
import org.mockito.kotlin.eq
import org.mockito.kotlin.mock
import org.mockito.kotlin.times
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
import org.robolectric.RobolectricTestRunner
import java.lang.reflect.Field
import java.lang.reflect.Modifier

@RunWith(RobolectricTestRunner::class)
class MenuItemFactoryTest {
Expand All @@ -31,8 +28,6 @@ class MenuItemFactoryTest {
private val rootView = mock<ExpandableBottomBar>()
private val styleController = mock<StyleController>()
private val onItemClick = mock<(MenuItem, View) -> Unit>()
private val colorStateList = mock<ColorStateList>()
private val drawableHelper = mockObject(DrawableHelper::class.java)

private val itemVerticalPadding: Int = 54
private val itemHorizontalPadding: Int = 61
Expand Down Expand Up @@ -80,7 +75,6 @@ class MenuItemFactoryTest {
@Before
fun setUp() {
whenever(rootView.context).thenReturn(ApplicationProvider.getApplicationContext())
doAnswer{ colorStateList }.whenever(drawableHelper).createSelectedUnselectedStateList(anyInt(), anyInt())
}

@Test
Expand All @@ -89,8 +83,8 @@ class MenuItemFactoryTest {

verify(itemView, times(1)).id = menuItemDescriptorWithoutNotificationInfo.itemId
verify(itemView, times(1)).setPadding(itemHorizontalPadding, itemVerticalPadding, itemHorizontalPadding, itemVerticalPadding)
verify(itemView, times(1)).setIcon(menuItemDescriptorWithoutNotificationInfo.iconId, colorStateList)
verify(itemView, times(1)).setText(menuItemDescriptorWithoutNotificationInfo.text, colorStateList)
verify(itemView, times(1)).setIcon(eq(menuItemDescriptorWithoutNotificationInfo.iconId), anyOrNull())
verify(itemView, times(1)).setText(eq(menuItemDescriptorWithoutNotificationInfo.text), anyOrNull())
verify(itemView, times(1)).notificationBadgeBackgroundColor = globalNotificationBadgeColor
verify(itemView, times(1)).notificationBadgeTextColor = globalNotificationBadgeTextColor
}
Expand All @@ -101,41 +95,9 @@ class MenuItemFactoryTest {

verify(itemView, times(1)).id = menuItemDescriptorWithNotificationInfo.itemId
verify(itemView, times(1)).setPadding(itemHorizontalPadding, itemVerticalPadding, itemHorizontalPadding, itemVerticalPadding)
verify(itemView, times(1)).setIcon(menuItemDescriptorWithNotificationInfo.iconId, colorStateList)
verify(itemView, times(1)).setText(menuItemDescriptorWithNotificationInfo.text, colorStateList)
verify(itemView, times(1)).setIcon(eq(menuItemDescriptorWithNotificationInfo.iconId), anyOrNull())
verify(itemView, times(1)).setText(eq(menuItemDescriptorWithNotificationInfo.text), anyOrNull())
verify(itemView, times(1)).notificationBadgeBackgroundColor = menuItemDescriptorWithNotificationInfo.badgeBackgroundColor!!
verify(itemView, times(1)).notificationBadgeTextColor = menuItemDescriptorWithNotificationInfo.badgeTextColor!!
}

private fun <T> mockObject(clazz: Class<T>): T {
val constructor = clazz.declaredConstructors.find { it.parameterCount == 0 }
?: throw InstantiationException("class ${clazz.canonicalName} has no empty constructor, " +
"is it really a Kotlin \"object\"?")

constructor.isAccessible = true

val mockedInstance = spy(constructor.newInstance() as T)

return replaceObjectInstance(clazz, mockedInstance)
}

private fun <T> replaceObjectInstance(clazz: Class<T>, newInstance: T): T {

if (!clazz.declaredFields.any {
it.name == "INSTANCE" && it.type == clazz && Modifier.isStatic(it.modifiers)
}) {
throw InstantiationException("clazz ${clazz.canonicalName} does not have a static " +
"INSTANCE field, is it really a Kotlin \"object\"?")
}

val instanceField = clazz.getDeclaredField("INSTANCE")
val modifiersField = Field::class.java.getDeclaredField("modifiers")
modifiersField.isAccessible = true
modifiersField.setInt(instanceField, instanceField.modifiers and Modifier.FINAL.inv())

instanceField.isAccessible = true
val originalInstance = instanceField.get(null) as T
instanceField.set(null, newInstance)
return newInstance
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,26 @@ package github.com.st235.expandablebottombar

import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import com.nhaarman.mockitokotlin2.*
import github.com.st235.lib_expandablebottombar.ExpandableBottomBar
import github.com.st235.lib_expandablebottombar.Menu
import github.com.st235.lib_expandablebottombar.MenuItemDescriptor
import github.com.st235.lib_expandablebottombar.MenuItemImpl
import github.com.st235.lib_expandablebottombar.components.MenuItemView
import github.com.st235.lib_expandablebottombar.utils.ConstraintLayoutHelper
import github.com.st235.lib_expandablebottombar.utils.TransitionHelper
import org.junit.Assert.*
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.mockito.kotlin.any
import org.mockito.kotlin.mock
import org.mockito.kotlin.never
import org.mockito.kotlin.times
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever

@RunWith(JUnit4::class)
class MenuItemImplTest {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package github.com.st235.expandablebottombar

import android.graphics.Color
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.times
import com.nhaarman.mockitokotlin2.verify
import com.nhaarman.mockitokotlin2.whenever
import github.com.st235.lib_expandablebottombar.Notification
import github.com.st235.lib_expandablebottombar.NotificationBadge
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.mockito.kotlin.mock
import org.mockito.kotlin.times
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever

@RunWith(JUnit4::class)
class NotificationTest {
Expand Down

0 comments on commit b0d03d2

Please sign in to comment.