无法 运行 Robolectric 和带有 FragmentContainerView 的 Espresso
Unable to run Robolectric and Espresso with a FragmentContainerView
我一直在尝试 运行 Robolectric 和 Espresso,但我还没有成功。在试图找出导致此问题的原因之后,我意识到 activity 中的 FragmentContainerView 导致了此问题,我不确定如何解决它。
我决定创建一个空白项目,我认为可能是我项目中的某些东西导致了这个问题,我在空白项目上遇到了同样的问题。
这是我正在测试的当前设置。
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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/mainActivityConstraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentContainerView"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/main_nav" />
</androidx.constraintlayout.widget.ConstraintLayout>
BlankFragment.java
public class BlankFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_blank, container, false);
}
}
fragment_blank.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BlankFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
</FrameLayout>
main_nav.xml
<?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/main_nav"
app:startDestination="@id/blankFragment">
<fragment
android:id="@+id/blankFragment"
android:name="com.example.robolectrictest.BlankFragment"
android:label="fragment_blank"
tools:layout="@layout/fragment_blank" />
</navigation>
build.gradle(项目)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:4.2.1"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
mavenCentral()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
build.gradle(模块)
plugins {
id 'com.android.application'
}
android {
compileSdkVersion 30
defaultConfig {
applicationId "com.example.robolectrictest"
minSdkVersion 27
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
testOptions {
animationsDisabled true
unitTests {
includeAndroidResources true
returnDefaultValues = true
}
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.navigation:navigation-fragment:2.3.5'
implementation 'androidx.navigation:navigation-ui:2.3.5'
testImplementation 'junit:junit:4.13.2'
testImplementation "org.robolectric:robolectric:4.5.1"
testImplementation 'androidx.test.espresso:espresso-core:3.3.0'
testImplementation 'androidx.test.ext:junit:1.1.2'
testImplementation 'androidx.arch.core:core-testing:2.1.0'
testImplementation "androidx.test:runner:1.3.0"
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
def nav_version = "2.3.5"
// Java language implementation
implementation "androidx.navigation:navigation-fragment:$nav_version"
implementation "androidx.navigation:navigation-ui:$nav_version"
// Feature module Support
implementation "androidx.navigation:navigation-dynamic-features-fragment:$nav_version"
// Testing Navigation
androidTestImplementation "androidx.navigation:navigation-testing:$nav_version"
// Jetpack Compose Integration
implementation "androidx.navigation:navigation-compose:2.4.0-alpha01"
}
MainActivityTest (androidTest):此仪器测试 运行s 在 emulator/device 中没有预期的问题。
@RunWith(AndroidJUnit4.class)
public class MainActivityTest {
@Rule
public ActivityScenarioRule<MainActivity> mainActivityRule =
new ActivityScenarioRule<MainActivity>(MainActivity.class);
@Test
public void testMainActivityIsVisible() {
onView(withId(R.id.mainActivityConstraintLayout)).check(matches(isDisplayed()));
}
}
MainActivityTest(测试):此测试无法 运行,它会抛出下面的异常,并且只有在我注释掉 activity_main.xml 布局中的 FragmentContainerView 时它才会起作用。
@RunWith(AndroidJUnit4.class)
public class MainActivityTest {
@Rule
public ActivityScenarioRule<MainActivity> mainActivityRule =
new ActivityScenarioRule<MainActivity>(MainActivity.class);
@Test
public void testMainActivityIsVisible() {
onView(withId(R.id.mainActivityConstraintLayout)).check(matches(isDisplayed()));
}
}
尝试 运行 使用 robolectric 和浓缩咖啡进行测试后收到异常:
java.lang.Exception: Main looper has queued unexecuted runnables. This might be the cause of the test failure. You might need a shadowOf(getMainLooper()).idle() call.
at org.robolectric.android.internal.AndroidTestEnvironment.checkStateAfterTestFailure(AndroidTestEnvironment.java:502)
at org.robolectric.RobolectricTestRunner$HelperTestRunner.evaluate(RobolectricTestRunner.java:581)
at org.robolectric.internal.SandboxTestRunner.lambda$evaluate[=21=](SandboxTestRunner.java:278)
at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread[=21=](Sandbox.java:89)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.ExceptionInInitializerError
at androidx.navigation.fragment.NavHostFragment.onCreate(NavHostFragment.java:219)
at androidx.fragment.app.Fragment.performCreate(Fragment.java:2949)
at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:475)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:278)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2106)
at androidx.fragment.app.FragmentManager.execSingleAction(FragmentManager.java:1971)
at androidx.fragment.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:311)
at androidx.fragment.app.FragmentContainerView.<init>(FragmentContainerView.java:180)
at androidx.fragment.app.FragmentLayoutInflaterFactory.onCreateView(FragmentLayoutInflaterFactory.java:52)
at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:135)
at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:319)
at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:298)
at android.view.LayoutInflater.tryCreateView(LayoutInflater.java:1067)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:995)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1121)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
at android.view.LayoutInflater.inflate(LayoutInflater.java:680)
at android.view.LayoutInflater.inflate(LayoutInflater.java:532)
at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:699)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:195)
at com.example.robolectrictest.MainActivity.onCreate(MainActivity.java:12)
at android.app.Activity.performCreate(Activity.java:7995)
at android.app.Activity.performCreate(Activity.java:7979)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at androidx.test.runner.MonitoringInstrumentation.callActivityOnCreate(MonitoringInstrumentation.java:711)
at org.robolectric.android.controller.ActivityController.lambda$create[=21=](ActivityController.java:106)
at org.robolectric.shadows.ShadowPausedLooper.runPaused(ShadowPausedLooper.java:200)
at org.robolectric.android.controller.ActivityController.create(ActivityController.java:106)
at org.robolectric.android.controller.ActivityController.create(ActivityController.java:111)
at org.robolectric.android.fakes.RoboMonitoringInstrumentation.startActivitySyncInternal(RoboMonitoringInstrumentation.java:66)
at org.robolectric.android.internal.LocalActivityInvoker.startActivity(LocalActivityInvoker.java:40)
at androidx.test.core.app.ActivityScenario.launchInternal(ActivityScenario.java:265)
at androidx.test.core.app.ActivityScenario.launch(ActivityScenario.java:195)
at androidx.test.ext.junit.rules.ActivityScenarioRule.lambda$new[=21=]$ActivityScenarioRule(ActivityScenarioRule.java:70)
at androidx.test.ext.junit.rules.ActivityScenarioRule$$Lambda[=21=].get(Unknown Source)
at androidx.test.ext.junit.rules.ActivityScenarioRule.before(ActivityScenarioRule.java:103)
at org.junit.rules.ExternalResource.evaluate(ExternalResource.java:50)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:306)
at org.robolectric.RobolectricTestRunner$HelperTestRunner.evaluate(RobolectricTestRunner.java:575)
... 6 more
Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at org.robolectric.internal.bytecode.ShadowWrangler.classInitializing(ShadowWrangler.java:166)
at org.robolectric.internal.bytecode.RobolectricInternals.classInitializing(RobolectricInternals.java:21)
at androidx.navigation.NavController.<clinit>(NavController.kt)
at androidx.navigation.fragment.NavHostFragment.$$robo$$androidx_navigation_fragment_NavHostFragment$onCreate(NavHostFragment.java:219)
at androidx.navigation.fragment.NavHostFragment.onCreate(NavHostFragment.java)
at androidx.fragment.app.Fragment.performCreate(Fragment.java:2949)
at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:475)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:278)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2106)
at androidx.fragment.app.FragmentManager.execSingleAction(FragmentManager.java:1971)
at androidx.fragment.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:311)
at androidx.fragment.app.FragmentContainerView.<init>(FragmentContainerView.java:180)
at androidx.fragment.app.FragmentLayoutInflaterFactory.onCreateView(FragmentLayoutInflaterFactory.java:52)
at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:135)
at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:319)
at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:298)
at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$tryCreateView(LayoutInflater.java:1067)
at android.view.LayoutInflater.tryCreateView(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$createViewFromTag(LayoutInflater.java:995)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$createViewFromTag(LayoutInflater.java:959)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$rInflate(LayoutInflater.java:1121)
at android.view.LayoutInflater.rInflate(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$rInflateChildren(LayoutInflater.java:1082)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$inflate(LayoutInflater.java:680)
at android.view.LayoutInflater.inflate(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$inflate(LayoutInflater.java:532)
at android.view.LayoutInflater.inflate(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$inflate(LayoutInflater.java:479)
at android.view.LayoutInflater.inflate(LayoutInflater.java)
at androidx.appcompat.app.AppCompatDelegateImpl.$$robo$$androidx_appcompat_app_AppCompatDelegateImpl$setContentView(AppCompatDelegateImpl.java:699)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java)
at androidx.appcompat.app.AppCompatActivity.$$robo$$androidx_appcompat_app_AppCompatActivity$setContentView(AppCompatActivity.java:195)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java)
at com.example.robolectrictest.MainActivity.onCreate(MainActivity.java:12)
at android.app.Activity.$$robo$$android_app_Activity$performCreate(Activity.java:7995)
at android.app.Activity.performCreate(Activity.java)
at android.app.Activity.$$robo$$android_app_Activity$performCreate(Activity.java:7979)
at android.app.Activity.performCreate(Activity.java)
at android.app.Instrumentation.$$robo$$android_app_Instrumentation$callActivityOnCreate(Instrumentation.java:1309)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
... 21 more
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.robolectric.internal.bytecode.RobolectricInternals.performStaticInitialization(RobolectricInternals.java:61)
at org.robolectric.internal.bytecode.ShadowWrangler.classInitializing(ShadowWrangler.java:163)
... 64 more
Caused by: java.lang.IllegalAccessError: failed to access class kotlin.jvm.internal.DefaultConstructorMarker from class androidx.navigation.NavController$Companion (kotlin.jvm.internal.DefaultConstructorMarker is in unnamed module of loader 'app'; androidx.navigation.NavController$Companion is in unnamed module of loader org.robolectric.internal.AndroidSandbox$SdkSandboxClassLoader @71def8f8)
at androidx.navigation.NavController$Companion.<init>(NavController.kt)
at androidx.navigation.NavController.__staticInitializer__(NavController.kt)
... 70 more
Process finished with exit code -1
所以,我尝试使用异常中的建议:
@RunWith(AndroidJUnit4.class)
@LooperMode(LooperMode.Mode.PAUSED)
public class MainActivityTest {
@Rule
public ActivityScenarioRule<MainActivity> mainActivityRule =
new ActivityScenarioRule<MainActivity>(MainActivity.class);
@Test
public void testMainActivityIsVisible() {
Shadows.shadowOf(Looper.getMainLooper()).idle();
onView(withId(R.id.mainActivityConstraintLayout)).check(matches(isDisplayed()));
}
}
但是我遇到了同样的异常:
java.lang.Exception: Main looper has queued unexecuted runnables. This might be the cause of the test failure. You might need a shadowOf(getMainLooper()).idle() call.
at org.robolectric.android.internal.AndroidTestEnvironment.checkStateAfterTestFailure(AndroidTestEnvironment.java:502)
at org.robolectric.RobolectricTestRunner$HelperTestRunner.evaluate(RobolectricTestRunner.java:581)
at org.robolectric.internal.SandboxTestRunner.lambda$evaluate[=23=](SandboxTestRunner.java:278)
at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread[=23=](Sandbox.java:89)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.ExceptionInInitializerError
at androidx.navigation.fragment.NavHostFragment.onCreate(NavHostFragment.java:219)
at androidx.fragment.app.Fragment.performCreate(Fragment.java:2949)
at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:475)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:278)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2106)
at androidx.fragment.app.FragmentManager.execSingleAction(FragmentManager.java:1971)
at androidx.fragment.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:311)
at androidx.fragment.app.FragmentContainerView.<init>(FragmentContainerView.java:180)
at androidx.fragment.app.FragmentLayoutInflaterFactory.onCreateView(FragmentLayoutInflaterFactory.java:52)
at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:135)
at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:319)
at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:298)
at android.view.LayoutInflater.tryCreateView(LayoutInflater.java:1067)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:995)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1121)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
at android.view.LayoutInflater.inflate(LayoutInflater.java:680)
at android.view.LayoutInflater.inflate(LayoutInflater.java:532)
at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:699)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:195)
at com.example.robolectrictest.MainActivity.onCreate(MainActivity.java:12)
at android.app.Activity.performCreate(Activity.java:7995)
at android.app.Activity.performCreate(Activity.java:7979)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at androidx.test.runner.MonitoringInstrumentation.callActivityOnCreate(MonitoringInstrumentation.java:711)
at org.robolectric.android.controller.ActivityController.lambda$create[=23=](ActivityController.java:106)
at org.robolectric.shadows.ShadowPausedLooper.runPaused(ShadowPausedLooper.java:200)
at org.robolectric.android.controller.ActivityController.create(ActivityController.java:106)
at org.robolectric.android.controller.ActivityController.create(ActivityController.java:111)
at org.robolectric.android.fakes.RoboMonitoringInstrumentation.startActivitySyncInternal(RoboMonitoringInstrumentation.java:66)
at org.robolectric.android.internal.LocalActivityInvoker.startActivity(LocalActivityInvoker.java:40)
at androidx.test.core.app.ActivityScenario.launchInternal(ActivityScenario.java:265)
at androidx.test.core.app.ActivityScenario.launch(ActivityScenario.java:195)
at androidx.test.ext.junit.rules.ActivityScenarioRule.lambda$new[=23=]$ActivityScenarioRule(ActivityScenarioRule.java:70)
at androidx.test.ext.junit.rules.ActivityScenarioRule$$Lambda[=23=].get(Unknown Source)
at androidx.test.ext.junit.rules.ActivityScenarioRule.before(ActivityScenarioRule.java:103)
at org.junit.rules.ExternalResource.evaluate(ExternalResource.java:50)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:306)
at org.robolectric.RobolectricTestRunner$HelperTestRunner.evaluate(RobolectricTestRunner.java:575)
... 6 more
Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at org.robolectric.internal.bytecode.ShadowWrangler.classInitializing(ShadowWrangler.java:166)
at org.robolectric.internal.bytecode.RobolectricInternals.classInitializing(RobolectricInternals.java:21)
at androidx.navigation.NavController.<clinit>(NavController.kt)
at androidx.navigation.fragment.NavHostFragment.$$robo$$androidx_navigation_fragment_NavHostFragment$onCreate(NavHostFragment.java:219)
at androidx.navigation.fragment.NavHostFragment.onCreate(NavHostFragment.java)
at androidx.fragment.app.Fragment.performCreate(Fragment.java:2949)
at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:475)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:278)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2106)
at androidx.fragment.app.FragmentManager.execSingleAction(FragmentManager.java:1971)
at androidx.fragment.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:311)
at androidx.fragment.app.FragmentContainerView.<init>(FragmentContainerView.java:180)
at androidx.fragment.app.FragmentLayoutInflaterFactory.onCreateView(FragmentLayoutInflaterFactory.java:52)
at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:135)
at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:319)
at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:298)
at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$tryCreateView(LayoutInflater.java:1067)
at android.view.LayoutInflater.tryCreateView(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$createViewFromTag(LayoutInflater.java:995)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$createViewFromTag(LayoutInflater.java:959)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$rInflate(LayoutInflater.java:1121)
at android.view.LayoutInflater.rInflate(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$rInflateChildren(LayoutInflater.java:1082)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$inflate(LayoutInflater.java:680)
at android.view.LayoutInflater.inflate(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$inflate(LayoutInflater.java:532)
at android.view.LayoutInflater.inflate(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$inflate(LayoutInflater.java:479)
at android.view.LayoutInflater.inflate(LayoutInflater.java)
at androidx.appcompat.app.AppCompatDelegateImpl.$$robo$$androidx_appcompat_app_AppCompatDelegateImpl$setContentView(AppCompatDelegateImpl.java:699)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java)
at androidx.appcompat.app.AppCompatActivity.$$robo$$androidx_appcompat_app_AppCompatActivity$setContentView(AppCompatActivity.java:195)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java)
at com.example.robolectrictest.MainActivity.onCreate(MainActivity.java:12)
at android.app.Activity.$$robo$$android_app_Activity$performCreate(Activity.java:7995)
at android.app.Activity.performCreate(Activity.java)
at android.app.Activity.$$robo$$android_app_Activity$performCreate(Activity.java:7979)
at android.app.Activity.performCreate(Activity.java)
at android.app.Instrumentation.$$robo$$android_app_Instrumentation$callActivityOnCreate(Instrumentation.java:1309)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
... 21 more
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.robolectric.internal.bytecode.RobolectricInternals.performStaticInitialization(RobolectricInternals.java:61)
at org.robolectric.internal.bytecode.ShadowWrangler.classInitializing(ShadowWrangler.java:163)
... 64 more
Caused by: java.lang.IllegalAccessError: failed to access class kotlin.jvm.internal.DefaultConstructorMarker from class androidx.navigation.NavController$Companion (kotlin.jvm.internal.DefaultConstructorMarker is in unnamed module of loader 'app'; androidx.navigation.NavController$Companion is in unnamed module of loader org.robolectric.internal.AndroidSandbox$SdkSandboxClassLoader @383bfa16)
at androidx.navigation.NavController$Companion.<init>(NavController.kt)
at androidx.navigation.NavController.__staticInitializer__(NavController.kt)
... 70 more
非常感谢任何帮助!
我想通了这个问题。导致这无法正常工作的原因是导航组件的以下依赖性。
// Jetpack Compose Integration
implementation "androidx.navigation:navigation-compose:2.4.0-alpha01"
在我删除它之后,它不仅解决了我的 robolectric 和 espresso 问题,还解决了另一个导航测试问题。我不确定最新版本中发生了什么,但是,由于我更新了特定的依赖项,它似乎与 robolectric 和导航测试都不兼容。
我一直在尝试 运行 Robolectric 和 Espresso,但我还没有成功。在试图找出导致此问题的原因之后,我意识到 activity 中的 FragmentContainerView 导致了此问题,我不确定如何解决它。
我决定创建一个空白项目,我认为可能是我项目中的某些东西导致了这个问题,我在空白项目上遇到了同样的问题。
这是我正在测试的当前设置。
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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/mainActivityConstraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentContainerView"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/main_nav" />
</androidx.constraintlayout.widget.ConstraintLayout>
BlankFragment.java
public class BlankFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_blank, container, false);
}
}
fragment_blank.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BlankFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
</FrameLayout>
main_nav.xml
<?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/main_nav"
app:startDestination="@id/blankFragment">
<fragment
android:id="@+id/blankFragment"
android:name="com.example.robolectrictest.BlankFragment"
android:label="fragment_blank"
tools:layout="@layout/fragment_blank" />
</navigation>
build.gradle(项目)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:4.2.1"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
mavenCentral()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
build.gradle(模块)
plugins {
id 'com.android.application'
}
android {
compileSdkVersion 30
defaultConfig {
applicationId "com.example.robolectrictest"
minSdkVersion 27
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
testOptions {
animationsDisabled true
unitTests {
includeAndroidResources true
returnDefaultValues = true
}
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.navigation:navigation-fragment:2.3.5'
implementation 'androidx.navigation:navigation-ui:2.3.5'
testImplementation 'junit:junit:4.13.2'
testImplementation "org.robolectric:robolectric:4.5.1"
testImplementation 'androidx.test.espresso:espresso-core:3.3.0'
testImplementation 'androidx.test.ext:junit:1.1.2'
testImplementation 'androidx.arch.core:core-testing:2.1.0'
testImplementation "androidx.test:runner:1.3.0"
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
def nav_version = "2.3.5"
// Java language implementation
implementation "androidx.navigation:navigation-fragment:$nav_version"
implementation "androidx.navigation:navigation-ui:$nav_version"
// Feature module Support
implementation "androidx.navigation:navigation-dynamic-features-fragment:$nav_version"
// Testing Navigation
androidTestImplementation "androidx.navigation:navigation-testing:$nav_version"
// Jetpack Compose Integration
implementation "androidx.navigation:navigation-compose:2.4.0-alpha01"
}
MainActivityTest (androidTest):此仪器测试 运行s 在 emulator/device 中没有预期的问题。
@RunWith(AndroidJUnit4.class)
public class MainActivityTest {
@Rule
public ActivityScenarioRule<MainActivity> mainActivityRule =
new ActivityScenarioRule<MainActivity>(MainActivity.class);
@Test
public void testMainActivityIsVisible() {
onView(withId(R.id.mainActivityConstraintLayout)).check(matches(isDisplayed()));
}
}
MainActivityTest(测试):此测试无法 运行,它会抛出下面的异常,并且只有在我注释掉 activity_main.xml 布局中的 FragmentContainerView 时它才会起作用。
@RunWith(AndroidJUnit4.class)
public class MainActivityTest {
@Rule
public ActivityScenarioRule<MainActivity> mainActivityRule =
new ActivityScenarioRule<MainActivity>(MainActivity.class);
@Test
public void testMainActivityIsVisible() {
onView(withId(R.id.mainActivityConstraintLayout)).check(matches(isDisplayed()));
}
}
尝试 运行 使用 robolectric 和浓缩咖啡进行测试后收到异常:
java.lang.Exception: Main looper has queued unexecuted runnables. This might be the cause of the test failure. You might need a shadowOf(getMainLooper()).idle() call.
at org.robolectric.android.internal.AndroidTestEnvironment.checkStateAfterTestFailure(AndroidTestEnvironment.java:502)
at org.robolectric.RobolectricTestRunner$HelperTestRunner.evaluate(RobolectricTestRunner.java:581)
at org.robolectric.internal.SandboxTestRunner.lambda$evaluate[=21=](SandboxTestRunner.java:278)
at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread[=21=](Sandbox.java:89)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.ExceptionInInitializerError
at androidx.navigation.fragment.NavHostFragment.onCreate(NavHostFragment.java:219)
at androidx.fragment.app.Fragment.performCreate(Fragment.java:2949)
at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:475)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:278)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2106)
at androidx.fragment.app.FragmentManager.execSingleAction(FragmentManager.java:1971)
at androidx.fragment.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:311)
at androidx.fragment.app.FragmentContainerView.<init>(FragmentContainerView.java:180)
at androidx.fragment.app.FragmentLayoutInflaterFactory.onCreateView(FragmentLayoutInflaterFactory.java:52)
at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:135)
at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:319)
at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:298)
at android.view.LayoutInflater.tryCreateView(LayoutInflater.java:1067)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:995)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1121)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
at android.view.LayoutInflater.inflate(LayoutInflater.java:680)
at android.view.LayoutInflater.inflate(LayoutInflater.java:532)
at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:699)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:195)
at com.example.robolectrictest.MainActivity.onCreate(MainActivity.java:12)
at android.app.Activity.performCreate(Activity.java:7995)
at android.app.Activity.performCreate(Activity.java:7979)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at androidx.test.runner.MonitoringInstrumentation.callActivityOnCreate(MonitoringInstrumentation.java:711)
at org.robolectric.android.controller.ActivityController.lambda$create[=21=](ActivityController.java:106)
at org.robolectric.shadows.ShadowPausedLooper.runPaused(ShadowPausedLooper.java:200)
at org.robolectric.android.controller.ActivityController.create(ActivityController.java:106)
at org.robolectric.android.controller.ActivityController.create(ActivityController.java:111)
at org.robolectric.android.fakes.RoboMonitoringInstrumentation.startActivitySyncInternal(RoboMonitoringInstrumentation.java:66)
at org.robolectric.android.internal.LocalActivityInvoker.startActivity(LocalActivityInvoker.java:40)
at androidx.test.core.app.ActivityScenario.launchInternal(ActivityScenario.java:265)
at androidx.test.core.app.ActivityScenario.launch(ActivityScenario.java:195)
at androidx.test.ext.junit.rules.ActivityScenarioRule.lambda$new[=21=]$ActivityScenarioRule(ActivityScenarioRule.java:70)
at androidx.test.ext.junit.rules.ActivityScenarioRule$$Lambda[=21=].get(Unknown Source)
at androidx.test.ext.junit.rules.ActivityScenarioRule.before(ActivityScenarioRule.java:103)
at org.junit.rules.ExternalResource.evaluate(ExternalResource.java:50)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:306)
at org.robolectric.RobolectricTestRunner$HelperTestRunner.evaluate(RobolectricTestRunner.java:575)
... 6 more
Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at org.robolectric.internal.bytecode.ShadowWrangler.classInitializing(ShadowWrangler.java:166)
at org.robolectric.internal.bytecode.RobolectricInternals.classInitializing(RobolectricInternals.java:21)
at androidx.navigation.NavController.<clinit>(NavController.kt)
at androidx.navigation.fragment.NavHostFragment.$$robo$$androidx_navigation_fragment_NavHostFragment$onCreate(NavHostFragment.java:219)
at androidx.navigation.fragment.NavHostFragment.onCreate(NavHostFragment.java)
at androidx.fragment.app.Fragment.performCreate(Fragment.java:2949)
at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:475)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:278)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2106)
at androidx.fragment.app.FragmentManager.execSingleAction(FragmentManager.java:1971)
at androidx.fragment.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:311)
at androidx.fragment.app.FragmentContainerView.<init>(FragmentContainerView.java:180)
at androidx.fragment.app.FragmentLayoutInflaterFactory.onCreateView(FragmentLayoutInflaterFactory.java:52)
at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:135)
at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:319)
at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:298)
at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$tryCreateView(LayoutInflater.java:1067)
at android.view.LayoutInflater.tryCreateView(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$createViewFromTag(LayoutInflater.java:995)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$createViewFromTag(LayoutInflater.java:959)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$rInflate(LayoutInflater.java:1121)
at android.view.LayoutInflater.rInflate(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$rInflateChildren(LayoutInflater.java:1082)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$inflate(LayoutInflater.java:680)
at android.view.LayoutInflater.inflate(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$inflate(LayoutInflater.java:532)
at android.view.LayoutInflater.inflate(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$inflate(LayoutInflater.java:479)
at android.view.LayoutInflater.inflate(LayoutInflater.java)
at androidx.appcompat.app.AppCompatDelegateImpl.$$robo$$androidx_appcompat_app_AppCompatDelegateImpl$setContentView(AppCompatDelegateImpl.java:699)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java)
at androidx.appcompat.app.AppCompatActivity.$$robo$$androidx_appcompat_app_AppCompatActivity$setContentView(AppCompatActivity.java:195)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java)
at com.example.robolectrictest.MainActivity.onCreate(MainActivity.java:12)
at android.app.Activity.$$robo$$android_app_Activity$performCreate(Activity.java:7995)
at android.app.Activity.performCreate(Activity.java)
at android.app.Activity.$$robo$$android_app_Activity$performCreate(Activity.java:7979)
at android.app.Activity.performCreate(Activity.java)
at android.app.Instrumentation.$$robo$$android_app_Instrumentation$callActivityOnCreate(Instrumentation.java:1309)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
... 21 more
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.robolectric.internal.bytecode.RobolectricInternals.performStaticInitialization(RobolectricInternals.java:61)
at org.robolectric.internal.bytecode.ShadowWrangler.classInitializing(ShadowWrangler.java:163)
... 64 more
Caused by: java.lang.IllegalAccessError: failed to access class kotlin.jvm.internal.DefaultConstructorMarker from class androidx.navigation.NavController$Companion (kotlin.jvm.internal.DefaultConstructorMarker is in unnamed module of loader 'app'; androidx.navigation.NavController$Companion is in unnamed module of loader org.robolectric.internal.AndroidSandbox$SdkSandboxClassLoader @71def8f8)
at androidx.navigation.NavController$Companion.<init>(NavController.kt)
at androidx.navigation.NavController.__staticInitializer__(NavController.kt)
... 70 more
Process finished with exit code -1
所以,我尝试使用异常中的建议:
@RunWith(AndroidJUnit4.class)
@LooperMode(LooperMode.Mode.PAUSED)
public class MainActivityTest {
@Rule
public ActivityScenarioRule<MainActivity> mainActivityRule =
new ActivityScenarioRule<MainActivity>(MainActivity.class);
@Test
public void testMainActivityIsVisible() {
Shadows.shadowOf(Looper.getMainLooper()).idle();
onView(withId(R.id.mainActivityConstraintLayout)).check(matches(isDisplayed()));
}
}
但是我遇到了同样的异常:
java.lang.Exception: Main looper has queued unexecuted runnables. This might be the cause of the test failure. You might need a shadowOf(getMainLooper()).idle() call.
at org.robolectric.android.internal.AndroidTestEnvironment.checkStateAfterTestFailure(AndroidTestEnvironment.java:502)
at org.robolectric.RobolectricTestRunner$HelperTestRunner.evaluate(RobolectricTestRunner.java:581)
at org.robolectric.internal.SandboxTestRunner.lambda$evaluate[=23=](SandboxTestRunner.java:278)
at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread[=23=](Sandbox.java:89)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.ExceptionInInitializerError
at androidx.navigation.fragment.NavHostFragment.onCreate(NavHostFragment.java:219)
at androidx.fragment.app.Fragment.performCreate(Fragment.java:2949)
at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:475)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:278)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2106)
at androidx.fragment.app.FragmentManager.execSingleAction(FragmentManager.java:1971)
at androidx.fragment.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:311)
at androidx.fragment.app.FragmentContainerView.<init>(FragmentContainerView.java:180)
at androidx.fragment.app.FragmentLayoutInflaterFactory.onCreateView(FragmentLayoutInflaterFactory.java:52)
at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:135)
at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:319)
at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:298)
at android.view.LayoutInflater.tryCreateView(LayoutInflater.java:1067)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:995)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1121)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
at android.view.LayoutInflater.inflate(LayoutInflater.java:680)
at android.view.LayoutInflater.inflate(LayoutInflater.java:532)
at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:699)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:195)
at com.example.robolectrictest.MainActivity.onCreate(MainActivity.java:12)
at android.app.Activity.performCreate(Activity.java:7995)
at android.app.Activity.performCreate(Activity.java:7979)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at androidx.test.runner.MonitoringInstrumentation.callActivityOnCreate(MonitoringInstrumentation.java:711)
at org.robolectric.android.controller.ActivityController.lambda$create[=23=](ActivityController.java:106)
at org.robolectric.shadows.ShadowPausedLooper.runPaused(ShadowPausedLooper.java:200)
at org.robolectric.android.controller.ActivityController.create(ActivityController.java:106)
at org.robolectric.android.controller.ActivityController.create(ActivityController.java:111)
at org.robolectric.android.fakes.RoboMonitoringInstrumentation.startActivitySyncInternal(RoboMonitoringInstrumentation.java:66)
at org.robolectric.android.internal.LocalActivityInvoker.startActivity(LocalActivityInvoker.java:40)
at androidx.test.core.app.ActivityScenario.launchInternal(ActivityScenario.java:265)
at androidx.test.core.app.ActivityScenario.launch(ActivityScenario.java:195)
at androidx.test.ext.junit.rules.ActivityScenarioRule.lambda$new[=23=]$ActivityScenarioRule(ActivityScenarioRule.java:70)
at androidx.test.ext.junit.rules.ActivityScenarioRule$$Lambda[=23=].get(Unknown Source)
at androidx.test.ext.junit.rules.ActivityScenarioRule.before(ActivityScenarioRule.java:103)
at org.junit.rules.ExternalResource.evaluate(ExternalResource.java:50)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:306)
at org.robolectric.RobolectricTestRunner$HelperTestRunner.evaluate(RobolectricTestRunner.java:575)
... 6 more
Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at org.robolectric.internal.bytecode.ShadowWrangler.classInitializing(ShadowWrangler.java:166)
at org.robolectric.internal.bytecode.RobolectricInternals.classInitializing(RobolectricInternals.java:21)
at androidx.navigation.NavController.<clinit>(NavController.kt)
at androidx.navigation.fragment.NavHostFragment.$$robo$$androidx_navigation_fragment_NavHostFragment$onCreate(NavHostFragment.java:219)
at androidx.navigation.fragment.NavHostFragment.onCreate(NavHostFragment.java)
at androidx.fragment.app.Fragment.performCreate(Fragment.java:2949)
at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:475)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:278)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2106)
at androidx.fragment.app.FragmentManager.execSingleAction(FragmentManager.java:1971)
at androidx.fragment.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:311)
at androidx.fragment.app.FragmentContainerView.<init>(FragmentContainerView.java:180)
at androidx.fragment.app.FragmentLayoutInflaterFactory.onCreateView(FragmentLayoutInflaterFactory.java:52)
at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:135)
at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:319)
at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:298)
at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$tryCreateView(LayoutInflater.java:1067)
at android.view.LayoutInflater.tryCreateView(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$createViewFromTag(LayoutInflater.java:995)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$createViewFromTag(LayoutInflater.java:959)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$rInflate(LayoutInflater.java:1121)
at android.view.LayoutInflater.rInflate(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$rInflateChildren(LayoutInflater.java:1082)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$inflate(LayoutInflater.java:680)
at android.view.LayoutInflater.inflate(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$inflate(LayoutInflater.java:532)
at android.view.LayoutInflater.inflate(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$inflate(LayoutInflater.java:479)
at android.view.LayoutInflater.inflate(LayoutInflater.java)
at androidx.appcompat.app.AppCompatDelegateImpl.$$robo$$androidx_appcompat_app_AppCompatDelegateImpl$setContentView(AppCompatDelegateImpl.java:699)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java)
at androidx.appcompat.app.AppCompatActivity.$$robo$$androidx_appcompat_app_AppCompatActivity$setContentView(AppCompatActivity.java:195)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java)
at com.example.robolectrictest.MainActivity.onCreate(MainActivity.java:12)
at android.app.Activity.$$robo$$android_app_Activity$performCreate(Activity.java:7995)
at android.app.Activity.performCreate(Activity.java)
at android.app.Activity.$$robo$$android_app_Activity$performCreate(Activity.java:7979)
at android.app.Activity.performCreate(Activity.java)
at android.app.Instrumentation.$$robo$$android_app_Instrumentation$callActivityOnCreate(Instrumentation.java:1309)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
... 21 more
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.robolectric.internal.bytecode.RobolectricInternals.performStaticInitialization(RobolectricInternals.java:61)
at org.robolectric.internal.bytecode.ShadowWrangler.classInitializing(ShadowWrangler.java:163)
... 64 more
Caused by: java.lang.IllegalAccessError: failed to access class kotlin.jvm.internal.DefaultConstructorMarker from class androidx.navigation.NavController$Companion (kotlin.jvm.internal.DefaultConstructorMarker is in unnamed module of loader 'app'; androidx.navigation.NavController$Companion is in unnamed module of loader org.robolectric.internal.AndroidSandbox$SdkSandboxClassLoader @383bfa16)
at androidx.navigation.NavController$Companion.<init>(NavController.kt)
at androidx.navigation.NavController.__staticInitializer__(NavController.kt)
... 70 more
非常感谢任何帮助!
我想通了这个问题。导致这无法正常工作的原因是导航组件的以下依赖性。
// Jetpack Compose Integration
implementation "androidx.navigation:navigation-compose:2.4.0-alpha01"
在我删除它之后,它不仅解决了我的 robolectric 和 espresso 问题,还解决了另一个导航测试问题。我不确定最新版本中发生了什么,但是,由于我更新了特定的依赖项,它似乎与 robolectric 和导航测试都不兼容。