2

I'm having trouble converting some of my old Robolectric 3.x tests to the new style (also converted to AndroidX at the same time).

My setup: - Android Gradle Plugin 3.2.1 - Robolectric 4.1

Some relevant config (gradle.properties):

android.useAndroidX=true
android.enableJetifier=false

Note, I am not using android.enableUnitTestBinaryResources (as it does not work on Windows in this version of the Android Gradle Plugin).

My fragment (snippet):

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    logger.debug(String.format("onCreateOptionsMenu(menu=[%s], inflater=[%s])", menu, inflater));

    inflater.inflate(R.menu.key_details_actions, menu);

key_details_actions.xml (full):

<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
        >

    <item
            android:id="@+id/action_handin"
            android:icon="@drawable/ic_return_key_wrapper"
            android:title="@string/action_key_hand_in"
            android:visible="false"
            app:showAsAction="ifRoom|withText"
            />
</menu>

ic_return_key_wrapper.xml (full):

<?xml version="1.0" encoding="utf-8"?>
<!-- https://stackoverflow.com/questions/35813199/how-can-i-use-vectordrawable-with-the-android-toolbar  -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_return_key"/>
</selector>

My test does the following (crashes):

final KeyDetailsActivity activity = Robolectric.buildActivity(KeyDetailsActivity.class)
        .create(instanceState).start().get();
activity.getSupportFragmentManager().beginTransaction()
        .add(R.id.container_key_fragment_details, fragment).commit();
final Context context = fragment.requireContext();
final Menu menu = new RoboMenu(context);

fragment.onCreateOptionsMenu(menu, new MenuInflater(context));

Stacktrace:

android.content.res.Resources$NotFoundException: File D:\Git\xxx\application\build\intermediates\merged-not-compiled-resources\debug\drawable\ic_return_key_wrapper.xml from drawable resource ID #0x7f0800d5

    at android.content.res.Resources.loadDrawable(Resources.java:2096)
    at org.robolectric.util.ReflectionHelpers.callInstanceMethod(ReflectionHelpers.java:283)
    at org.robolectric.internal.bytecode.ShadowImpl.directlyOn(ShadowImpl.java:56)
    at org.robolectric.shadow.api.Shadow.directlyOn(Shadow.java:56)
    at org.robolectric.shadows.ShadowResources.loadDrawable(ShadowResources.java:231)
    at android.content.res.Resources.loadDrawable(Resources.java)
    at android.content.res.Resources.getDrawable(Resources.java:700)
    at org.robolectric.fakes.RoboMenuItem.setIcon(RoboMenuItem.java:107)
    at android.view.MenuInflater$MenuState.setItem(MenuInflater.java:399)
    at android.view.MenuInflater$MenuState.addItem(MenuInflater.java:451)
    at android.view.MenuInflater.parseMenu(MenuInflater.java:188)
    at android.view.MenuInflater.inflate(MenuInflater.java:110)
Alix
  • 2,630
  • 30
  • 72

1 Answers1

0

You also should put to android DSL next lines:

   testOptions {
     unitTests {
       returnDefaultValues = true
       includeAndroidResources = true
     }
   }
Eugen Martynov
  • 19,888
  • 10
  • 61
  • 114