我正在使用Navigation Component
为一个包含一个Activity和多个片段的应用编写一些插装测试。
启动画面的代码如下所示:
class SplashFragment : Fragment(), KodeinAware {
override val kodein by Admin.instance.kodein
private var realm: Realm? = null
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.splash, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
(activity as? AppCompatActivity)?.supportActionBar?.hide()
realm = Realm.getInstance(RealmUtil.realmConfig)
val result = realm!!.where<User>().findFirst()
val user = if (result != null) realm!!.copyFromRealm(result) else null
Handler().postDelayed({
if (user == null)
findNavController().navigate(R.id.action_splashFragment_to_loginFragment) //navigate to login screen if no user exists
else
findNavController().navigate(R.id.action_splashFragment_to_businessListFragment) //navigate to business list if user already logged in
}, 2000)
}
override fun onDestroy() {
super.onDestroy()
realm?.close()
}
}
我尝试测试闪屏后出现的片段,但不断收到以下错误:
java.lang.IllegalStateException: Fragment SplashFragment{a1ca381 (5f5b98ae-c130-4e9b-9b77-0495561ef4f5)} not associated with a fragment manager.
at androidx.fragment.app.Fragment.requireFragmentManager(Fragment.java:891)
at androidx.navigation.fragment.NavHostFragment.findNavController(NavHostFragment.java:106)
at androidx.navigation.fragment.FragmentKt.findNavController(Fragment.kt:29)
at com.chargebot.collect.admin.fragment.onboarding.SplashFragment$onViewCreated$1.run(SplashFragment.kt:44)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at androidx.test.espresso.base.Interrogator.loopAndInterrogate(Interrogator.java:148)
at androidx.test.espresso.base.UiControllerImpl.loopUntil(UiControllerImpl.java:519)
at androidx.test.espresso.base.UiControllerImpl.loopUntil(UiControllerImpl.java:478)
at androidx.test.espresso.base.UiControllerImpl.injectKeyEvent(UiControllerImpl.java:201)
at androidx.test.espresso.base.UiControllerImpl.injectString(UiControllerImpl.java:357)
at androidx.test.espresso.action.TypeTextAction.perform(TypeTextAction.java:108)
at androidx.test.espresso.ViewInteraction$SingleExecutionViewAction.perform(ViewInteraction.java:360)
at androidx.test.espresso.ViewInteraction.doPerform(ViewInteraction.java:251)
at androidx.test.espresso.ViewInteraction.access$100(ViewInteraction.java:64)
at androidx.test.espresso.ViewInteraction$1.call(ViewInteraction.java:157)
at androidx.test.espresso.ViewInteraction$1.call(ViewInteraction.java:154)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
我的测试类如下:
@RunWith(AndroidJUnit4::class)
@MediumTest
class SignInTest {
@get: Rule
val login = ActivityScenarioRule(Home::class.java)
@Test
fun loginWithoutEmail_ShouldDisplayError() {
val scenario = launchFragmentInContainer<LoginFragment>()
onView(withId(R.id.password)).perform(typeText("samplePassword"), closeSoftKeyboard())
onView(withId(R.id.login)).perform(click())
onView(withId(com.google.android.material.R.id.snackbar_text)).check(matches(withText(R.string.enter_email)))
}
@Test
fun loginWithoutPassword_ShouldDisplayError() {
val scenario = launchFragmentInContainer<LoginFragment>()
onView(withId(R.id.email)).perform(typeText("sample@password.com"), closeSoftKeyboard())
onView(withId(R.id.login)).perform(click())
onView(withId(com.google.android.material.R.id.snackbar_text)).check(matches(withText(R.string.enter_password)))
}
}
由于上述错误,我的测试函数都无法执行。我在启动画面上执行的测试成功后,发生异常的原因是什么?
我的nav_graph
如下:
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_graph"
app:startDestination="@id/splashFragment">
<fragment
android:id="@+id/splashFragment"
android:name="com.chargebot.collect.admin.fragment.onboarding.SplashFragment"
android:label="SplashFragment"
tools:layout="@layout/splash">
<action
android:id="@+id/action_splashFragment_to_loginFragment"
app:destination="@id/loginFragment"
app:popUpTo="@+id/splashFragment"
app:popUpToInclusive="true" />
<action
android:id="@+id/action_splashFragment_to_businessListFragment"
app:destination="@id/businessListFragment"
app:popUpTo="@+id/splashFragment"
app:popUpToInclusive="true" />
</fragment>
<fragment
android:id="@+id/registrationFragment"
android:name="com.chargebot.collect.admin.fragment.onboarding.RegistrationFragment"
android:label="RegistrationFragment"
tools:layout="@layout/registration">
<action
android:id="@+id/action_registrationFragment_to_businessListFragment"
app:destination="@id/businessListFragment"
app:popUpTo="@+id/registrationFragment"
app:popUpToInclusive="true" />
<action
android:id="@+id/action_registrationFragment_to_loginFragment"
app:destination="@id/loginFragment"
app:popUpTo="@+id/registrationFragment"
app:popUpToInclusive="true" />
</fragment>
<fragment
android:id="@+id/loginFragment"
android:name="com.chargebot.collect.admin.fragment.onboarding.LoginFragment"
android:label="LoginFragment"
tools:layout="@layout/login">
<action
android:id="@+id/action_loginFragment_to_businessListFragment"
app:destination="@id/businessListFragment"
app:popUpTo="@+id/loginFragment"
app:popUpToInclusive="true" />
<action
android:id="@+id/action_loginFragment_to_registrationFragment"
app:destination="@id/registrationFragment"
app:popUpTo="@+id/loginFragment"
app:popUpToInclusive="true" />
</fragment>
<fragment
android:id="@+id/businessListFragment"
android:name="com.chargebot.collect.admin.fragment.business.BusinessListFragment"
android:label="BusinessListFragment"
tools:layout="@layout/businesses_list">
<action
android:id="@+id/action_businessListFragment_to_newBusinessFragment"
app:destination="@id/newBusinessFragment" />
<action
android:id="@+id/action_businessListFragment_to_branchesFragment"
app:destination="@id/branchesFragment" />
</fragment>
<fragment
android:id="@+id/newBusinessFragment"
android:name="com.chargebot.collect.admin.fragment.business.NewBusinessFragment"
android:label="NewBusinessFragment"
tools:layout="@layout/add_business_layout">
<action
android:id="@+id/action_newBusinessFragment_to_businessListFragment"
app:destination="@id/businessListFragment" />
</fragment>
<fragment
android:id="@+id/branchesFragment"
android:name="com.chargebot.collect.admin.fragment.branch.BranchesFragment"
android:label="BranchesFragment"
tools:layout="@layout/branches_layout">
<action
android:id="@+id/action_branchesFragment_to_transactionsFragment"
app:destination="@id/transactionsFragment" />
<action
android:id="@+id/action_branchesFragment_to_newBranchFragment"
app:destination="@id/newBranchFragment" />
<action
android:id="@+id/action_branchesFragment_to_collectorsFragment"
app:destination="@id/collectorsFragment" />
</fragment>
<fragment
android:id="@+id/transactionsFragment"
android:name="com.chargebot.collect.admin.fragment.transactions.TransactionsFragment"
android:label="TransactionsFragment"
tools:layout="@layout/transactions" />
<fragment
android:id="@+id/newBranchFragment"
android:name="com.chargebot.collect.admin.fragment.branch.NewBranchFragment"
android:label="NewBranchFragment"
tools:layout="@layout/add_branch_layout" />
<fragment
android:id="@+id/collectorsFragment"
android:name="com.chargebot.collect.admin.fragment.collector.CollectorsFragment"
android:label="CollectorsFragment"
tools:layout="@layout/client_list_layout">
<action
android:id="@+id/action_collectorsFragment_to_newCollectorFragment"
app:destination="@id/newCollectorFragment" />
<action
android:id="@+id/action_collectorsFragment_to_transactionsFragment"
app:destination="@id/transactionsFragment" />
<action
android:id="@+id/action_collectorsFragment_to_collectorTransactions"
app:destination="@id/collectorTransactions" />
</fragment>
<fragment
android:id="@+id/newCollectorFragment"
android:name="com.chargebot.collect.admin.fragment.collector.NewCollectorFragment"
android:label="NewCollectorFragment"
tools:layout="@layout/new_client_layout" />
<fragment
android:id="@+id/collectorTransactions"
android:name="com.chargebot.collect.admin.fragment.transactions.CollectorTransactionsFragment"
android:label="CollectorTransactions"
tools:layout="@layout/transaction_layout">
<action
android:id="@+id/action_collectorTransactions_to_collectorsFragment"
app:destination="@id/collectorsFragment"
app:popUpTo="@id/collectorsFragment" />
</fragment>
</navigation>
5条答案
按热度按时间bqucvtff1#
我发现,在我的情况下,这个问题与线程有关。我用以下方法解决了这个问题:
如果这对你有帮助的话会很好的?!
wooyq4lh2#
在您的代码下面添加注解
如果一切正常的话
使用
Thread.sleep(5000)
或IdlingResource
在您的仪器测试用例中进行延迟。**根本原因:**实际上,在测试用例运行时期间,您正在尝试对等待Handler执行2000毫秒的片段进行操作。
qni6mghb3#
我刚刚在测试中发现了这个问题
通过遵循navigation test documentation,我能够解决使用navController为片段创建场景的问题
oewdyzsn4#
此链接很有用... https://www.py4u.net/discuss/667013
我创建了侦听器并将其作为
但是没有删除它。因为它不是lifeyle感知的,所以即使在片段被销毁后,它仍然有对片段的引用。所以在Ramkrishna Joshi的帮助下,我删除了
onDestroyView()
中的侦听器,错误消失了。
如果您在片段或Activity中使用生命周期未知的侦听器,请在适当的位置手动销毁它们。
wgeznvg75#
我正在使用类似这样的代码,希望这能解决问题