Robolectric 2.0 does not create views when testing

I am trying to set up testing using robolectric and roboguice. I successfully inject and test classes, but when I try to validate actions, I cannot create View objects in a test environment.

I get the following errors:

testAttemptsLoginWhenSignInButtonIsClicked (com.clearc2.HomeActivityTest) Elapsed time: 2.244 s <ERROR! java.lang.RuntimeException: crash for creating android.widget.EditText in org.robolectric.res.builder.LayoutBuilder.constructView (LayoutBuilder.java:182) in org.robolectric.res.builder.LayoutBuilder.create (LayoutBuilder.java: 109) in org.robolectric.res.builder.LayoutBuilder.doInflate (LayoutBuilder.java:42) in org.robolectric.res.builder.LayoutBuilder.doInflate (LayoutBuilder.java:45) in org.robolectric.res.builder.LayoutBuilder .doInflate (LayoutBuilder.java:45) in org.robolectric.res.builder.LayoutBuilder.doInflate (LayoutBuilder.java:45) in org.robolectric.res.builder.LayoutBuilder.inflateView (LayoutBuilder.java:62) in org. robolectric.shadows.ShadowLayoutInflater.inflate (ShadowLayoutInflater.java:50) at org.robolectric.shadows.ShadowLayoutInflater.inflate (ShadowLayoutInflater.java:55) on android.view.LayoutInflater.inflate (LayoutInflater.java.org) .android.view.RoboWindow.setContentView (RoboWindow.java:80) in org.robolectric.shad ows.ShadowActivity.setContentView (ShadowActivity.java:265) in android.app.Activity.setContentView (Activity.java) in com.clearc2.HomeActivity.onCreate (HomeActivity.java:38) in com.clearc2.HomeActivityTest.setUp (HomeActivityTest .java: 22) in org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall (FrameworkMethod.java:44) in org.junit.internal.runners.model.ReflectiveCallable.run (ReflectiveCallable.java:15) in org.junit .runners.model.FrameworkMethod.invokeExplosively (FrameworkMethod.java:41) in org.junit.internal.runners.statements.RunBefores.evaluate (RunBefores.java:27) in org.junit.internal.runners.statements.RunAfters.evaluate (RunAfters.java:31) in org.robolectric.RobolectricTestRunner $ 2.evaluate (RobolectricTestRunner.java:246) in org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored (BlockJUnit4ClassRunner.java:79runnerunnerunnerunnerunnerunnerunnerunner (BlockJUnit4ClassRunner.java:71) in org.junit.runners.BlockJUnit4ClassRunner.runChild (BlockJUnit4 ClassRunner.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) in org.junit.runners.ParentRunner.access $ 000 (ParentRunner.java:42) in org.junit.runners.ParentRunner $ 2.valuation (ParentRunner.java:184) in org.robolectric. RobolectricTestRunner $ 1. Compute value (RobolectricTestRunner.java:181) on org.junit.runners.ParentRunner.run (ParentRunner.java:236) in org.apache.maven.surefire.junit4.JUnit4TestSet.execute (JUnit4TestSet.java:59) apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet (AbstractDirectoryTestSuite.java:120) at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute (AbstractDirectoryTestSuite.java.sf.urem run (Surefire.java:169) at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess (SurefireBooter.javahaps50) at org.apache.maven.surefire.booter.SurefireBooter.main (SurefireBooter.java10) Called: java.lang.reflect.InvocationTargetException when sun.reflect.NativeConstructorAccessorImpl.newInstance0 (native method) in sun.reflect.NativeConstructorAccessorImpl.newInstance (NativeConstructorAccessorImpl.java:57) in sun.reflectectorflectlectorlectplleplpllectlectlectlectlectorlectpllepllepllepllepllepllepllepllepllepllepllepllepllepllepllepllepllepllepllepllepllepllectorpllepllepllepllepllectorpllectorpllectorpllelectlelectpllectlelectlelectlelectlelectlelectlelectlelectlelectlelectlelectlelectlelectlelectlelectpllectlector Instance (DelegatingConstructorAccessorImpl.java:45) in java.lang.reflect.Constructor.newInstance (Constructor.java∗25) in org.robolectric.res.builder.LayoutBuilder.constructView (LayoutBuilder.java:167) in 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 ) in org.robolectric.res.builder.LayoutBuilder.doInflate (LayoutBuilder.java:45) in org.robolectric.res.builder.LayoutBuilder.doInflate (LayoutBuilder.java:45) in org.robolectric.res.builder.LayoutBuilder. inflateView (LayoutBuilder.java:62) in org.robolectric.shadows.ShadowLayoutInflater.inflate (ShadowLayoutInflater.java:50) in org.robolectric.shadows.ShadowLayoutInflater.inflate (ShadowLayoutInflater.Java: 55) at sun.reflectlect (Native method) in sun.reflect.NativeMethodAccessorImpl.invoke (NativeMeth odAccessorImpl.java:57) in sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43) in java.lang.reflect.Method.invoke (Method.java:601) in org.robolectric.bytecode.ShadowWrangler $ ShadowrethodPlan (ShadowWrangler.java:440) on android.view.LayoutInflater.inflate (LayoutInflater.java) on org.robolectric.tester.android.view.RoboWindow.setContentView (RoboWindow.java:80) in org.robolectric.shadows.ShadowActivity. setContentView (ShadowActivity.java:265) at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native method) in sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:57) in sun.reflect.DelegatingMethodccessPloc in java.lang.reflect.Method.invoke (Method.java:601) in org.robolectric.bytecode.ShadowWrangler $ ShadowMethodPlan.run (ShadowWrangler.java:440) in android.app.Activity.setContententView (Activity.java) in com.clearc2.HomeActivity.onCreate (HomeActivity.java : 38) in com.clearc2.HomeActivityTest.setUp (HomeActivityTest.java:22) in sun.reflect.NativeMethodAccessorImpl.invoke0 (native method) in sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.jref). DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43) in java.lang.reflect.Method.invoke (Method.java:601) in org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall (FrameworkMethod.java:44). junit.internal.runners.model.ReflectiveCallable.run (ReflectiveCallable.java:15) in org.junit.runners.model.FrameworkMethod.invokeExplosively (FrameworkMethod.java:41) in org.junit.internal.runners.statements.RunBefores. evaluate (RunBefores.java:27) in org.junit.internal.runners.statements.RunAfters.evaluate (RunAfters.java:31) in org.robolectric.RobolectricTestRunner $ 2.evaluate (RobolectricTestRunner.java:246) in org.junit. runners.BlockJUnit4ClassRunner.runNotIgnored (BlockJUnit4ClassRunner.java:79) in org.jun it. 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.valuation (ParentRunner.java:184) at org.robolectric.RobolectricTestRunner $ 1. Compute value (RobolectricTestRunner.java:181) on org.junit.runners.ParentRunner.run (ParentRunner.java:236) in org.apache.maven.surefire.junit4.JUnit4TestSet.execute (JUnit4TestSet.java:59) apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet (AbstractDirectoryTestSuite.java:120) at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute (AbstractDirectoryTestSuite.java.sf.urem run (Surefire.java:169) at sun.reflect.NativeMethodAccessorImpl.invoke0 (native method) in sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:57) in sun.reflect.DelegatingMethodAccessorImatingplinvoke in java.lang.reflect.Method.invoke (Method.java:601) ... 2 more Called: java.lang.NumberFormatException: for input line: "@ 0" at java.lang.NumberFormatException.forInputString (NumberFormatException.java : 65) in java.lang.Integer.parseInt (Integer.java:492) in com.android.internal.util.XmlUtils.convertValueToInt (XmlUtils.java:123) in android.content.res.TypedArray.getInt (TypedArray.java:254 ) at android.widget.TextView. Constructor (TextView.java:827) in android.widget.TextView. (TextView.java:447) in android.widget.EditText. (EditText.java:60) at android.widget.EditText. (EditText.java:56) in org.robolectric.res.builder.LayoutBuilder.constructView (LayoutBuilder.java:167) in org.robolectric.res.builder.LayoutBuilder.create (LayoutBuilder.java:109) in 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 45) in org.robolectric.res.builder.LayoutBuilder.doInflate (LayoutBuilder.java:45) in org.robolectric.res.builder.LayoutBuilder.inflateView (LayoutBuilder.java:62) in org.robolectric.shadows.ShadowLayoutInflater.infl (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) in org.robolectric.shadows.ShadowActivity.setContentView (ShadowActivity.java:265) in an droid.app.Activity.setContentView (Activity.java) in com.clearc2.HomeActivity.onCreate (HomeActivity.java:38) in com.clearc2.HomeActivityTest.setUp (HomeActivityTest.java:22) in org.junit.runners.model .FrameworkMethod $ 1.runReflectiveCall (FrameworkMethod.java:44) in org.junit.internal.runners.model.ReflectiveCallable.run (ReflectiveCallable.java:15) in org.junit.runners.model.FrameworkMethod.invokeExplosively (Framework 41) in org.junit.internal.runners.statements.RunBefores.evaluate (RunBefores.java:27) in org.junit.internal.runners.statements.RunAfters.evaluate (RunAfters.java:31) in org.robolectric.RobolectricTestRunner $ 2. .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.valuation (ParentRunner.java:184) at org.robolectric.RobolectricTestRunner $ 1. Compute value (RobolectricTestRunner.java:181) on org.junit.runners.ParentRunner.run (ParentRunner.java:236) in org.apache.maven.surefire.junit4.JUnit4TestSet.execute (JUnit4TestSet.java:59) apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet (AbstractDirectoryTestSuite.java:120) in org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute (AbstractDirectoryTestSuite.javaurefire.me run (Surefire.java:169) ... 2 more

+4
source share
2 answers

found a problem. Robolectric did not like the android ime settings set on my EditText password. As soon as I deleted these lines, the test passed as expected.

+6
source

I had similar errors, but the problem was imeActionId , not imeOptions . Removing imeActionId from XML and setting it to code (but saving imeOptions ) solved this problem.

Here are my mistakes for reference and for search robots so people can more easily find this answer:

 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" ... 

I think Robolectric cannot parse imeActionId correctly for some reason. This seems to be related to the problem described in this question: Why setting imeActionId with a predefined identifier resource generates an error?

+2
source

Source: https://habr.com/ru/post/1484699/


All Articles