gpt4 book ai didi

Robolectric 2.0 在测试时不创建 View

转载 作者:行者123 更新时间:2023-12-04 02:54:38 25 4
gpt4 key购买 nike

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

我收到以下错误

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

最佳答案

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

关于Robolectric 2.0 在测试时不创建 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16947181/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com