Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
358 views
in Technique[技术] by (71.8m points)

kotlin - Mock a custom Toast in Android

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

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

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...