【问题标题】:Robolectric 2.0 not creating views when testingRobolectric 2.0 在测试时不创建视图
【发布时间】:2013-06-01 13:57:11
【问题描述】:

我正在尝试使用 robolectric 和 roboguice 进行测试。我已成功注入和测试类,但是当我尝试测试活动时,我似乎无法在测试环境中创建 View 对象

我收到以下错误

testAttemptsLoginWhenSignInButtonIsClicked(com.clearc2.HomeActivityTest) 经过时间:2.234 秒 constructor(TextView.java:827) 在 android.widget.TextView.(TextView.java:447) 在 android.widget.EditText.(EditText.java:60) 在 android.widget.EditText.(EditText.java:56) 在 org.robolectric.res.builder.LayoutBuilder.constructView(LayoutBuilder.java:167) 在 org.robolectric.res.builder.LayoutBuilder.create(LayoutBuilder.java:109) 在 org.robolectric.res.builder.LayoutBuilder.doInflate(LayoutBuilder.java:42) 在 org.robolectric.res.builder.LayoutBuilder.doInflate(LayoutBuilder.java:45) 在 org.robolectric.res.builder.LayoutBuilder.doInflate(LayoutBuilder.java:45) 在 org.robolectric.res.builder.LayoutBuilder.doInflate(LayoutBuilder.java:45) 在 org.robolectric.res.builder.LayoutBuilder.inflateView(LayoutBuilder.java:62) 在 org.robolectric.shadows.ShadowLayoutInflater.inflate(ShadowLayoutInflater.java:50) 在 org.robolectric.shadows.ShadowLayoutInflater.inflate(ShadowLayoutInflater.java:55) 在 android.view.LayoutInflater.inflate(LayoutInflater.java) 在 org.robolectric.tester.android.view.RoboWindow.setContentView(RoboWindow.java:80) 在 org.robolectric.shadows.ShadowActivity.setContentView(ShadowActivity.java:265) 在 android.app.Activity.setContentView(Activity.java) 在 com.clearc2.HomeActivity.onCreate(HomeActivity.java:38) 在 com.clearc2.HomeActivityTest.setUp(HomeActivityTest.java:22) 在 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) 在 org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 在 org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) 在 org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27) 在 org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) 在 org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:246) 在 org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) 在 org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) 在 org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) 在 org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) 在 org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) 在 org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) 在 org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:181) 在 org.junit.runners.ParentRunner.run(ParentRunner.java:236) 在 org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:59) 在 org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:120) 在 org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:103) 在 org.apache.maven.surefire.Surefire.run(Surefire.java:169) ... 2 更多

【问题讨论】:

    标签: robolectric roboguice


    【解决方案1】:

    发现了问题。 Robolectric 不喜欢在我的密码 EditText 上设置的 android ime 选项。一旦我删除了这些行,测试就会按预期运行。

    【讨论】:

    • 这是正确答案,对我帮助很大。那谢谢啦。并且不要忘记将其标记为正确答案。干杯。
    【解决方案2】:

    我也遇到过类似的错误,但问题是imeActionId,而不是imeOptions。从 XML 中删除 imeActionId 并在代码中设置它(但保留 imeOptions)解决了这个问题。

    这是我的错误供参考和搜索引擎爬虫,以便人们更容易找到这个答案:

    android.view.InflateException: XML file app/build/intermediates/res/debug/layout/fragment_registration_form.xml 
    line #-1 (sorry, not yet implemented): Error inflating class com.company.project.widgets.MyCustomEditTextView
    ...
    Caused by: java.lang.reflect.InvocationTargetException
    ...
    Caused by: java.lang.NumberFormatException: For input string: "@0"
    ...
    

    我猜 Robolectric 出于某种原因无法正确解析 imeActionId。看起来它与此问题中描述的问题有关:Why does setting imeActionId with a predefined ID resource create an error?

    【讨论】:

      猜你喜欢
      • 2016-07-05
      • 1970-01-01
      • 2013-08-15
      • 2018-02-12
      • 2013-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多