4

I've some problems using Robolectric with an AppCompatActivity containing NavigationVIew.

I found some post which relate this but nothing help me

for exemple this one : Robolectric 3.0 not working with AppCompat 21+ or https://github.com/robolectric/robolectric/issues/1859

My step :

My gradle :

compileSdkVersion 23
buildToolsVersion "23.0.2"
...
minSdkVersion 14
targetSdkVersion 23
...
testCompile 'junit:junit:4.12'

    testCompile('org.robolectric:robolectric:3.0+') {
        exclude group: 'commons-logging', module: 'commons-logging'
        exclude group: 'org.apache.httpcomponents', module: 'httpclient'
        exclude group: 'com.android.support', module: 'appcompat'
        exclude group: 'com.android.support', module: 'support-v4'
        exclude group: 'com.android.support', module: 'recyclerview-v7'
    }
    testCompile "org.robolectric:shadows-support-v4:3.0"
    compile 'com.android.support:appcompat-v7:23.2.0'
    compile 'com.android.support:design:23.2.0'
    compile 'com.android.support:support-v4:23.2.0'

I added a project.properties ( and also a test-project.proerties) containing this :

android.library.reference.1=../../build/intermediates/exploded-aar/com.android.support/appcompat-v7/23.2.0
android.library.reference.2=../../build/intermediates/exploded-aar/com.android.support/support-v4/23.2.0

My MainActivity

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    public static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Other stuff (init toolbar etc..)
   }
}

My MainActivityTest

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP)
public class MainActivityTest {

    private MainActivity activity;

    @Before
    public void setup() {
        // Convenience method to run MainActivity through the Activity Lifecycle methods:
        // onCreate(...) => onStart() => onPostCreate(...) => onResume()
        activity = Robolectric.setupActivity(MainActivity.class);
    }

    @Test
    public void testLoadDefaultFragment() {
        Fragment fragment = activity.getSupportFragmentManager()
                .findFragmentByTag(HomeFragment.TAG);

        assertTrue(fragment instanceof HomeFragment);
    }
}

And I still have this error :

android.view.InflateException: XML file build\intermediates\res\merged\app\debug\layout\activity_main.xml line #-1 (sorry, not yet implemented): Error inflating class android.support.design.widget.NavigationView
    at android.view.LayoutInflater.createView(LayoutInflater.java:633)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
    at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:267)
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:129)
    at com.***.***.Activities.MainActivity.onCreate(MainActivity.java:43)
...

THe line 43 in MainActivity.java is :

setContentView(R.layout.activity_main);

And here is my activity_main :

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

NB : I tried Robolectric 3.0, 3.0+, 3.1-SNAPSHOT to have SDK 23 support, nothing works

Thank's for your help.

Community
  • 1
  • 1
Nanis
  • 361
  • 6
  • 18
  • I fixed it by changing one vector image used in my project. I don't know why, but some vector assets are not recognize on Android Lollipop. (I found the solution when I tested the app on multiple platform) So this wasn't a Robolectric bug. – Nanis Apr 21 '16 at 11:59

0 Answers0