Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Robolectric 2.0 not creating views when testing

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

like image 306
David Avatar asked Feb 17 '23 08:02

David


1 Answers

found the problem. Robolectric didn't like the android ime options set on my password EditText. As soon as i removed those lines the test ran as expected.

like image 78
David Avatar answered Feb 27 '23 03:02

David