title: How do you test a case of Layout Binding in Mockito or any Unit Test (for coverage)?
body: <p>I am currently looking for a way to test a couple of methods, and I am using Layout Bindings.</p>
  <pre><code>buildFeatures {
      viewBinding true
  }
  </code></pre>
  <p>After most login, at some point, the code interacts with the binding and it throws a Null Pointer, I debugged a bit and noticed that the spy(k) Fragment's Binding is null, and even if I were to do the same logic inside the activity its binding is null as well.</p>
  <p>Any ideas/ advice can provide more source code if necessary.</p>
  <pre><code>class MockKitoTestJune {
  
      @BeforeEach
      fun setUp() = MockKAnnotations.init(this)
  
      @Before
      fun init() {
  //        mockStatic(PatternsCompat::class.java)
  
          mockkStatic(Looper::class)
          every { Looper.myLooper() } returns mockk()
  
          mockkStatic(Log::class)
          every { Log.v(any(), any()) } returns 0
          every { Log.d(any(), any()) } returns 0
          every { Log.i(any(), any()) } returns 0
          every { Log.e(any(), any()) } returns 0
  
          MockitoAnnotations.openMocks(this)
      }
  
  
      @Test
      fun testMockK() {
          //Code that uses the mock here
          val email = &quot;String@gmail.com&quot;
          val password = &quot;String&quot;
          val fullName = &quot;String&quot;
  
          // given
          val signUpFragment = spyk&lt;SignUpFragment&gt;()
  
          doReturn(signUpFragment.validateForm(email, password, fullName)).`when`(true)
          doReturn(signUpFragment.validateForm(&quot;&quot;, password, fullName)).`when`(false)
          doReturn(signUpFragment.validateForm(null, password, fullName)).`when`(false)
          doReturn(signUpFragment.validateForm(email, &quot;&quot;, fullName)).`when`(false)
          doReturn(signUpFragment.validateForm(email, password, &quot;&quot;)).`when`(false)
  
          // when checking mocked method
          val goodResult = signUpFragment.validateForm(email, password, fullName)
          val firstResult = signUpFragment.validateForm(&quot;&quot;, password, fullName)
  
          // then
          assertThat(goodResult).isTrue()
          assertThat(firstResult).isFalse()
      }
  }
  </code></pre>
  <p>In the logic when it gets to this -</p>
  <pre><code>binding.editTextFullName.error = &quot;You need to enter a name&quot;
  </code></pre>
  <p>This is part of the Fragment:</p>
  <pre><code>private var _binding: FragmentSignUpBinding? = null
  val binding get() = _binding!!
  
  
    override fun onCreateView(
  _binding = FragmentSignUpBinding.inflate(inflater, container, false)
  
  .
  .
  .
  </code></pre>