Roboelectric 3.0与Android Studio android.view.InflateException

时间:2015-09-03 21:18:20

标签: gradle robolectric

我最近搬到了 Roboelectric 3.0,我无法通过一个简单的步骤来运行一个运行以下命令的测试:

@Config(sdk = 18, constants = BuildConfig.class) 
@RunWith(RobolectricGradleTestRunner.class)
public class LegDetailActivityTest {
    @Test public void testThatLegDetailActivityCanBeInjected() {    

        LegDetailsActivity activity =
            Robolectric.buildActivity(LegDetailsActivity.class).create(bundle).get();

        graph.inject(activity);
    }
}

我收到以下错误:

android.view.InflateException: XML file build/intermediates/res/merged/debug/layout/abc_screen_toolbar.xml line #-1 (sorry, not yet implemented): Error inflating class <unknown>
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.rInflate(LayoutInflater.java:809)
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.ensureSubDecor(AppCompatDelegateImplV7.java:299)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:246)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)

我的活动课程已扩展为AppCompatActivity

我的build.gradle有这些依赖关系:

  testCompile 'junit:junit:4.12'
  testCompile 'org.easytesting:fest:1.0.16'
  testCompile 'com.squareup:fest-android:1.0.8'
  testCompile 'org.mockito:mockito-all:1.9.5'
  testCompile('org.robolectric:robolectric:3.0') {
    exclude group: 'commons-logging', module: 'commons-logging'
    exclude group: 'org.apache.httpcomponents', module: 'httpclient'
  }
  testCompile('org.robolectric:shadows-support-v4:3.0')

有没有人遇到过这个问题?

1 个答案:

答案 0 :(得分:2)

问题出在v4库的某个地方。我通过在build.gradle中排除v4来解决它。

 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'
  }