4

I'm trying to set up testing with robolectric and roboguice. I'm successfully injecting and testing classes but when i try to test activities i can't seem to create View objects in the test environment

I get the following errors

testAttemptsLoginWhenSignInButtonIsClicked(com.clearc2.HomeActivityTest) Time elapsed: 2.234 sec <<< ERROR! java.lang.RuntimeException: Failed to create a android.widget.EditText at org.robolectric.res.builder.LayoutBuilder.constructView(LayoutBuilder.java:182) at org.robolectric.res.builder.LayoutBuilder.create(LayoutBuilder.java:109) at org.robolectric.res.builder.LayoutBuilder.doInflate(LayoutBuilder.java:42) at org.robolectric.res.builder.LayoutBuilder.doInflate(LayoutBuilder.java:45) at org.robolectric.res.builder.LayoutBuilder.doInflate(LayoutBuilder.java:45) at org.robolectric.res.builder.LayoutBuilder.doInflate(LayoutBuilder.java:45) at org.robolectric.res.builder.LayoutBuilder.inflateView(LayoutBuilder.java:62) at org.robolectric.shadows.ShadowLayoutInflater.inflate(ShadowLayoutInflater.java:50) at org.robolectric.shadows.ShadowLayoutInflater.inflate(ShadowLayoutInflater.java:55) at android.view.LayoutInflater.inflate(LayoutInflater.java) at org.robolectric.tester.android.view.RoboWindow.setContentView(RoboWindow.java:80) at org.robolectric.shadows.ShadowActivity.setContentView(ShadowActivity.java:265) at android.app.Activity.setContentView(Activity.java) at 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) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:246) at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:181) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:59) at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:120) at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:103) at org.apache.maven.surefire.Surefire.run(Surefire.java:169) at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:350) at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1021) Caused by: java.lang.reflect.InvocationTargetException at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:525) at org.robolectric.res.builder.LayoutBuilder.constructView(LayoutBuilder.java:167) at org.robolectric.res.builder.LayoutBuilder.create(LayoutBuilder.java:109) at org.robolectric.res.builder.LayoutBuilder.doInflate(LayoutBuilder.java:42) at org.robolectric.res.builder.LayoutBuilder.doInflate(LayoutBuilder.java:45) at org.robolectric.res.builder.LayoutBuilder.doInflate(LayoutBuilder.java:45) at org.robolectric.res.builder.LayoutBuilder.doInflate(LayoutBuilder.java:45) at org.robolectric.res.builder.LayoutBuilder.inflateView(LayoutBuilder.java:62) at org.robolectric.shadows.ShadowLayoutInflater.inflate(ShadowLayoutInflater.java:50) at org.robolectric.shadows.ShadowLayoutInflater.inflate(ShadowLayoutInflater.java:55) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at org.robolectric.bytecode.ShadowWrangler$ShadowMethodPlan.run(ShadowWrangler.java:440) at android.view.LayoutInflater.inflate(LayoutInflater.java) at org.robolectric.tester.android.view.RoboWindow.setContentView(RoboWindow.java:80) at org.robolectric.shadows.ShadowActivity.setContentView(ShadowActivity.java:265) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at org.robolectric.bytecode.ShadowWrangler$ShadowMethodPlan.run(ShadowWrangler.java:440) at android.app.Activity.setContentView(Activity.java) at 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) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:246) at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:181) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:59) at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:120) at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:103) at org.apache.maven.surefire.Surefire.run(Surefire.java:169) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) ... 2 more Caused by: java.lang.NumberFormatException: For input string: "@0" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:492) at com.android.internal.util.XmlUtils.convertValueToInt(XmlUtils.java:123) at android.content.res.TypedArray.getInt(TypedArray.java:254) at android.widget.TextView.constructor(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) at org.robolectric.res.builder.LayoutBuilder.create(LayoutBuilder.java:109) at org.robolectric.res.builder.LayoutBuilder.doInflate(LayoutBuilder.java:42) at org.robolectric.res.builder.LayoutBuilder.doInflate(LayoutBuilder.java:45) at org.robolectric.res.builder.LayoutBuilder.doInflate(LayoutBuilder.java:45) at org.robolectric.res.builder.LayoutBuilder.doInflate(LayoutBuilder.java:45) at org.robolectric.res.builder.LayoutBuilder.inflateView(LayoutBuilder.java:62) at org.robolectric.shadows.ShadowLayoutInflater.inflate(ShadowLayoutInflater.java:50) at org.robolectric.shadows.ShadowLayoutInflater.inflate(ShadowLayoutInflater.java:55) at android.view.LayoutInflater.inflate(LayoutInflater.java) at org.robolectric.tester.android.view.RoboWindow.setContentView(RoboWindow.java:80) at org.robolectric.shadows.ShadowActivity.setContentView(ShadowActivity.java:265) at android.app.Activity.setContentView(Activity.java) at 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) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:246) at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:181) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:59) at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:120) at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:103) at org.apache.maven.surefire.Surefire.run(Surefire.java:169) ... 2 more

4

2 回答 2

6

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

于 2013-06-05T20:11:38.387 回答
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。看起来它与这个问题中描述的问题有关:为什么使用预定义的 ID 资源设置 imeActionId 会产生错误?

于 2014-12-03T04:43:26.687 回答