如何解决尝试膨胀具有TextInputLayout的布局时,robolectric测试失败 科特林实施 Java实现
Android Studio 4.0.1
Robolectric 4.3.1
我正在编写自动机械测试,该测试将失败,并显示以下错误。似乎与无法夸大的TextInputLayout有关。如果我删除TextInputLayout,则测试将通过。
这是我收到的错误消息。
android.view.InflateException: Binary XML file line #47: Binary XML file line #47: Error inflating class com.google.android.material.textfield.TextInputLayout
Caused by: android.view.InflateException: Binary XML file line #47: Error inflating class com.google.android.material.textfield.TextInputLayout
Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.AppCompat (or a descendant).
测试类本身
@Config(sdk = [Build.VERSION_CODES.O_MR1])
@RunWith(AndroidJUnit4::class)
class CharitiesAdapterTest {
private lateinit var charitiesAdapter: CharitiesAdapter
@Before
fun setUp() {
charitiesAdapter = CharitiesAdapter()
}
@Test
fun `should create viewHolder`() {
// Act & Assert
assertThat(createViewHolder()).isNotNull
}
private fun createViewHolder(): CharitiesViewHolder {
val constraintLayout = ConstraintLayout(ApplicationProvider.getApplicationContext())
return charitiesAdapter.onCreateViewHolder(constraintLayout,0)
}
}
正在测试的实际适配器
class CharitiesAdapter : RecyclerView.Adapter<CharitiesViewHolder>() {
private val charitiesList: MutableList<Charity> = mutableListOf()
private var selectedCharity: (Charity) -> Unit = {}
override fun onCreateViewHolder(parent: ViewGroup,viewType: Int): CharitiesViewHolder {
val charitiesViewHolder = CharitiesViewHolder(
LayoutInflater.from(parent.context).inflate(R.layout.charity_item,parent,false))
return charitiesViewHolder
}
override fun getItemCount(): Int = charitiesList.count()
override fun onBindViewHolder(holder: CharitiesViewHolder,position: Int) {
// left blank
}
}
这是布局中出现问题的部分。仅删除TextInputLayout时,测试将通过OK
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textInputLayoutPostcode"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/ivLogo">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/editTextPostcode"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</com.google.android.material.textfield.TextInputLayout>
这是我正在使用的样式
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
谢谢您的建议
解决方法
您可以将测试配置为使用使用应用程序主题的模拟应用程序,如下所示:
科特林实施
class TestApplication : Application() {
override fun onCreate() {
super.onCreate()
setTheme(R.style.AppTheme) //or just R.style.Theme_AppCompat
}
}
然后将您的测试配置为使用此模拟应用程序
@Config(application=TestApplication::class,sdk = [Build.VERSION_CODES.O_MR1])
@RunWith(AndroidJUnit4::class)
class CharitiesAdapterTest {
Java实现
public class TestApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
setTheme(R.style.AppTheme); //or just R.style.Theme_AppCompat
}
}
然后将您的测试配置为使用此模拟应用程序
@Config(application=TestApplication.class,sdk = [Build.VERSION_CODES.O_MR1])
@RunWith(AndroidJUnit4.class)
class CharitiesAdapterTest {
,
您传递了错误的Context
类:ApplicationProvider.getApplicationContext()
。ApplicationContext
根本没有主题,使用了Activity
建议的Context
(无论哪个{{1} }或Activity
。根据提供的代码,您不清楚如何启动Activity
(应该可以访问Theme
)。
使用ActivityScenario
或ActivityScenarioRule
进行测试可能是正确的方法。
文档显示了AndroidX Test和JUnit4 rules with AndroidX Test的工作方式。 / p>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。