1

I have a simple test function like this:

@Test
fun testfunc() {
    val fragScenario = launchFragmentInContainer<MyFragment>()
}

launchFragmentInContainer provides me with this error: Binary XML file line #10: Failed to resolve attribute at index 1: TypedValue{t=0x2/d=0x7f040004 a=-1} Caused by: java.lang.UnsupportedOperationException

which takes me to something wrong with the binding inflater.

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    _binding = MyFragmentBinding.inflate(inflater, container, false)
    return binding.root
}

I assume view binding doesn't work with isolated fragments like this? Do I need to create an entire new fragment without view binding just for testing?

  • did you already add the `@RunWith(AndroidJUnit4::class)` in your test class? Found there's an example here (but not sure if the ViewBinding is supported): https://developer.android.com/guide/fragments/test#dialog – mochadwi Jul 09 '21 at 15:50
  • This might related to your question about testing the `ViewBinding Fragment` in here: https://stackoverflow.com/a/62542185/3763032, you might also wanna give a shot with `PowerMock`(?) – mochadwi Jul 09 '21 at 16:12

1 Answers1

0

In the fragment testing documentation there is a note:

Note: Your fragment might require a theme that the test activity doesn't use by default. You can provide your own theme as an additional argument to launch() and launchInContainer().

This error normally occurs when there are style values that the activity isn't aware of. Be sure to pass your app or fragment style via the themeResId parameter.

sethwhite
  • 286
  • 3
  • 11