Robolectric: “AndroidManifest.xml not found” and "Unable to find resource ID #0x7f09001b"

Rob*_*iro 1 android compiler-errors android-manifest robolectric

I'm running some tests with Roboletric, but I came across a issue that I can't solve. When I run the test, the following error appears with the "AndroidManifest":

WARNING: No manifest file found at .\AndroidManifest.xml.

Falling back to the Android OS resources only. To remove this warning, annotate your test class with @Config(manifest=Config.NONE).

No such manifest file: .\AndroidManifest.xml

I've tried these solutions that failed:

@Config (manifest = Config.DEFAULT_MANIFEST_NAME)

@Config(manifest = Config.NONE, constants = BuildConfig.class, sdk = 26)

@Config( constants = BuildConfig.class, manifest="src/main/AndroidManifest.xml", sdk = 26 )
Run Code Online (Sandbox Code Playgroud)

And the other error during execution is:

android.content.res.Resources$NotFoundException: Unable to find resource ID #0x7f09001b in packages [android, org.robolectric.default]

...

at

com.example.robertoassad.alltestsmerge.MainActivity.onCreate(MainActivity.java:52)

This line that have the error is the following code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
Run Code Online (Sandbox Code Playgroud)

Specifically in: setContentView(R.layout.activity_main);

对我来说,在这个问题上我没有感觉...

细节:

  • 测试类位于文件夹中: app\src\test\java\com\example\robertoassad

  • 测试是: @RunWith( RobolectricTestRunner.class) public class Roboletric { @Test public void clickingLogin_shouldStartLoginActivity() { MainActivity activity = Robolectric.setupActivity(MainActivity.class); activity.findViewById(R.id.button2).performClick();

            Intent expectedIntent = new Intent(activity, SecondActivity.class);
            Intent actual = ShadowApplication.getInstance().getNextStartedActivity();
            assertEquals(expectedIntent.getComponent(), actual.getComponent());
         }
    
    Run Code Online (Sandbox Code Playgroud)

    }

Jaw*_*een 5

我遇到了与您面临的问题类似的问题。在通过jongerrish后关于这个Robolectric GitHub的问题解决了这个问题对我来说。

对我有用的答案是testOptions在模块build.gradle文件中添加一个块:

testOptions {
    unitTests {
        includeAndroidResources = true
    }
}
Run Code Online (Sandbox Code Playgroud)

添加此块后,我的测试能够运行和访问String资源。