https://kotlinlang.org logo
#mockk
Title
# mockk
v

Vivek Modi

07/12/2021, 4:06 PM
Hey i am doing testing this function
Copy code
internal fun graphView(viewGroup: ViewGroup, orientation: graphOrientaion = GraphOrientaion.HORIZONTAL): View {
    return when (orientation) {
        GraphOrientaion.HORIZONTAL -> {
            LayoutInflater.from(viewGroup.context).inflate(R.layout.horizaontal_layout, viewGroup, false)
        }
        GraphOrientaion.VERTICAL -> {
            LayoutInflater.from(viewGroup.context).inflate(R.layout.vertical_layout, viewGroup, false)
        }
    }
}
i added this test cases
Copy code
@Test
fun `graphView - HORIZONTAL view`() {
    val mockOrientation = GraphOrientaion.HORIZONTAL
    every {
        LayoutInflater.from(mockContext).inflate(
            R.layout.horizaontal_layout,
            mockViewGroup,
            false
        )
    } returns mockView
    // EXECUTION
    val view = graphView(mockViewGroup, mockOrientation)
    // VERIFICATION
    assertEquals(mockView, view)
}
what i am doing wrong
java.lang.NullPointerException
at com.vivek$graphView - HORIZONTAL view$1.invoke(TestClass.kt:306)
at com.vivek$graphView - HORIZONTAL view$1.invoke(TestClass.kt:31)
s

Sourabh Rawat

07/13/2021, 8:10 AM
Check the lines mentioned in the stacktrace. And Try debugging.
My guess is
viewGroup.context
is null/not set in the
mockViewGroup
v

Vivek Modi

07/13/2021, 8:29 AM
viewGroup.context
is not null because it working in another test
e

ephemient

07/14/2021, 1:43 PM
unless you're using Robolectric (which has other issues with mockk), LayoutInflater.from will act like all other Android APIs and either throw or return null, depending on test configuration
v

Vivek Modi

07/14/2021, 1:44 PM
i am not using Robolectric
r

Reprator

08/29/2021, 9:44 AM
Bro, it's ui testing not unit testing
6 Views