导航单元测试中的TextView InflateException

时间:2020-06-23 22:31:51

标签: android unit-testing navigation expresso

我正在尝试测试应用程序的导航。

@RunWith(AndroidJUnit4::class)
class CreateAccountTest {

@Test
fun init(){
    val mockNavController = mock(NavController::class.java)

    val titleScenario = launchFragmentInContainer<CreateAccountPage1>()

    titleScenario.onFragment { fragment ->
        Navigation.setViewNavController(fragment.requireView(), mockNavController)
    }

    // Verify that performing a click prompts the correct Navigation action
    onView(withId(R.id.bt_create_accont)).perform(click())
    verify(mockNavController).navigate(R.id.action_createAccountPage1_to_createAccountPage2)
 }
}

XML:

<androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<ImageView
    android:id="@+id/imageView19"
    android:layout_width="wrap_content"
    android:layout_height="240dp"
    android:layout_marginTop="16dp"
    android:src="@drawable/ic_ilu_account_creation"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/textView226"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:text="..."
    android:textColor="?text_color_dark"
    android:textSize="18sp"
    android:gravity="center"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/imageView19" />

<TextView
    android:id="@+id/textView227"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:layout_marginEnd="16dp"
    android:text="..."
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView226" />

<Button
    app:layout_constraintVertical_bias="0.35"
    app:backgroundTint="?button_link_color"
    android:id="@+id/bt_create_accont"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="..."
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView227" />

<Button
    android:id="@+id/bt_voltar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:text="..."
    android:textColor="?text_color_dark"
    app:backgroundTint="?backgroundColor"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/bt_create_accont" />

</androidx.constraintlayout.widget.ConstraintLayout>

堆栈遮罩:

java.lang.RuntimeException: android.view.InflateException: Binary XML file line #16 in 
.../fragment_create_account_page1: Binary XML file line #16 in 
.../fragment_create_account_page1: Error inflating class <unknown>

...

Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 5: TypedValue{t=0x2/d=0x7f04045d a=-1}

看起来问题出在这行`android:textColor =“?text_color_dark”`

为什么不支持此功能?我使用属性为浅色和深色主题设置应用程序的颜色/图标,默认值在清单中设置为浅色主题。

testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:core:1.2.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test:rules:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
androidTestImplementation 'org.mockito:mockito-core:2.23.4'
androidTestImplementation 'org.mockito:mockito-android:2.23.4'
debugImplementation 'androidx.fragment:fragment-testing:1.3.0-alpha06'
debugImplementation 'androidx.fragment:fragment-ktx:1.3.0-alpha06'

1 个答案:

答案 0 :(得分:0)

我能够通过在launchFragmentInContainer中指定主题来解决此问题

launchFragmentInContainer<CreateAccountPage1>(null, R.style.myAwesomeTheme)