Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am new to the Mocking Framework, I had been trying to mock a custom Toast for my application. Her is the code for it :


   fun buildToastMessage(context: Context, message: String): Boolean {
       try {
       val mToast = Toast(context)
val inflater: LayoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as 
                                                                                            LayoutInflater
    val layout = inflater.inflate(R.layout.toast_view, (context as Activity).findViewById(R.id.toast_container))
                   layout.findViewById<TextView>(R.id.toast_text).text = message
                   mToast.apply {
                       setGravity(Gravity.CENTER, 0, 0)
                       duration = Toast.LENGTH_SHORT
                       view = layout
                       show()
                   }
                   return true
               } catch (e: Exception) {
                   e.printStackTrace()
               }

It Used the Mockito framework and I am not able to access the inner functionalities of the function during the test. I get a error message.

  

@Test
   fun `Testing Toast Message`() {

       `when`(mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).thenReturn(inflater)
       val actual = mViewHelper.buildToastMessage(mContext, "Toast Message")
       assertEquals(true, actual)

   }

And My Test fails and This is the log that I get :

at org.junit.Assert.fail(Assert.java:89)
        at org.junit.Assert.failNotEquals(Assert.java:835)
        at org.junit.Assert.assertEquals(Assert.java:120)
        at org.junit.Assert.assertEquals(Assert.java:146)
        at com.adaptavant.yoco.unittest.helper.ViewHelperTest.Testing Toast Message(ViewHelperTest.kt:65)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:688)
        at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
        at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
question from:https://stackoverflow.com/questions/65942749/mock-a-custom-toast-in-android

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
371 views
Welcome To Ask or Share your Answers For Others

1 Answer

Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...