3

when I run the tests in robolectric 1.1 the following error occurs when inflate a layout file:

   java.lang.RuntimeException: Could not find layout layout/home_layout
       at com.xtremelabs.robolectric.res.ViewLoader.inflateView(ViewLoader.java:92)
       at com.xtremelabs.robolectric.res.ViewLoader.inflateView(ViewLoader.java:82)
       at com.xtremelabs.robolectric.res.ViewLoader.inflateView(ViewLoader.java:86)

Does anyone know why this error occurs? from what I saw, in version 1.1 of robolectric was no change in the way of loading layout files, it searches the folder layout/:

   private ViewNode getViewNodeByLayoutName(String layoutName) {
        if (layoutName.startsWith("layout/") && !qualifierSearchPath.isEmpty()) {
            String rawLayoutName = layoutName.substring("layout/".length());
            for (String location : qualifierSearchPath) {
                ViewNode foundNode = viewNodesByLayoutName.get("layout-" + location + "/" + rawLayoutName);
                if (foundNode != null) {
                    return foundNode;
                }
            }
        }
        return viewNodesByLayoutName.get(layoutName);
    }

but if the layout file is in another directory (eg layout-normal-hdpi), this error will occur ..

Does anyone know any solution for this?

1 Answers1

0

Qualified Resources

In the scenario described above, in RoboElectric 2.4 (perhaps earlier versions; I haven't checked), it's possible to specify a qualifier for the resource. See the RoboElectric page on Qualified Resources.

For example, if there are resources:

 layout/settings_config.xml
 layout-normal/settings_config.xml
 layout-normal-hdpi/settings_config.xml

... then the attribute @Config(qualifiers="normal-hdpi") would ensure that the layout-normal-hdpi/settings_config.xml resource would be used.

Changing resource paths using a different RobolectricTestRunner

A different approach (probably not for use in this scenario, but good if resource paths have been changed in build.gradle) is to load different resources as part of the implementation of the RobolectricTestRunner. There's a good example of how to do this here:

https://stackoverflow.com/a/29223625/3063884

Community
  • 1
  • 1
CJBS
  • 15,147
  • 6
  • 86
  • 135