Robolectric 2.0在测试时不会创建视图

时间:2013-06-05 18:23:47

标签: robolectric roboguice

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

我收到以下错误

  

testAttemptsLoginWhenSignInButtonIsClicked(com.clearc2.HomeActivityTest)   经过的时间:2.234秒<<<错误! java.lang.RuntimeException:失败   在。创建一个android.widget.EditText   org.robolectric.res.builder.LayoutBuilder.constructView(LayoutBuilder.java:182)     在   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)at   com.clearc2.HomeActivityTest.setUp(HomeActivityTest.java:22)at   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)at   org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:52)at at   org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)at at   org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:42)at at   org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:184)at at   org.robolectric.RobolectricTestRunner $ 1.evaluate(RobolectricTestRunner.java:181)     在org.junit.runners.ParentRunner.run(ParentRunner.java:236)at   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)at   org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:350)     在   org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1021)   引起:java.lang.reflect.InvocationTargetException at   sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)     在   sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)     在   sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)     在java.lang.reflect.Constructor.newInstance(Constructor.java:525)     在   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)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:601)at   org.robolectric.bytecode.ShadowWrangler $ ShadowMethodPlan.run(ShadowWrangler.java:440)     在android.view.LayoutInflater.inflate(LayoutInflater.java)处   org.robolectric.tester.android.view.RoboWindow.setContentView(RoboWindow.java:80)     在   org.robolectric.shadows.ShadowActivity.setContentView(ShadowActivity.java:265)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:601)at   org.robolectric.bytecode.ShadowWrangler $ ShadowMethodPlan.run(ShadowWrangler.java:440)     在android.app.Activity.setContentView(Activity.java)处   com.clearc2.HomeActivity.onCreate(HomeActivity.java:38)at   com.clearc2.HomeActivityTest.setUp(HomeActivityTest.java:22)at   sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:601)at   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)at   org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:52)at at   org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)at at   org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:42)at at   org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:184)at at   org.robolectric.RobolectricTestRunner $ 1.evaluate(RobolectricTestRunner.java:181)     在org.junit.runners.ParentRunner.run(ParentRunner.java:236)at   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)at   sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:601)...还有2个   引起:java.lang.NumberFormatException:对于输入字符串:" @ 0"在   java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)     在java.lang.Integer.parseInt(Integer.java:492)at   com.android.internal.util.XmlUtils.convertValueToInt(XmlUtils.java:123)     在android.content.res.TypedArray.getInt(TypedArray.java:254)at   android.widget.TextView。构造函数(TextView.java:827)at   android.widget.TextView。(TextView.java:447)at   android.widget.EditText。(EditText.java:60)at   android.widget.EditText。(EditText.java:56)at   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)at   com.clearc2.HomeActivityTest.setUp(HomeActivityTest.java:22)at   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)at   org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:52)at at   org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)at at   org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:42)at at   org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:184)at at   org.robolectric.RobolectricTestRunner $ 1.evaluate(RobolectricTestRunner.java:181)     在org.junit.runners.ParentRunner.run(ParentRunner.java:236)at   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   更

2 个答案:

答案 0 :(得分:6)

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

答案 1 :(得分: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?