以下是我的测试类:
package net.therap.passreset.service;import androID.content.Context;import com.xtremelabs.robolectric.Robolectric;import net.therap.passreset.util.ConnectivityStatus;import org.junit.After;import org.junit.Before;import org.junit.Rule;import org.junit.Test;import org.junit.runner.RunWith;import org.powermock.core.classloader.annotations.PrepareForTest;import org.powermock.modules.junit4.rule.PowerMockRule;import roboguice.RoboGuice;import roboguice.activity.RoboActivity;import roboguice.test.RobolectricRoboTestRunner;import static org.easymock.EasyMock.expect;import static org.powermock.API.easymock.PowerMock.mockStatic;/** * @author sharafat */@RunWith(RobolectricRoboTestRunner.class)@PrepareForTest(ConnectivityStatus.class)public class TherapWebServiceClIEntTest { @Rule public PowerMockRule rule = new PowerMockRule(); private Context context = new RoboActivity(); private TherapWebServiceClIEnt therapWebServiceClIEnt; @Before public voID setUp() throws Exception { RoboGuice.setBaseApplicationInjector(Robolectric.application,RoboGuice.DEFAulT_STAGE); therapWebServiceClIEnt = RoboGuice.getInjector(context).getInstance(TherapWebServiceClIEnt.class); mockStatic(ConnectivityStatus.class); } @After public voID tearDown() throws Exception { RoboGuice.util.reset(); } @Test public voID testLogin_success() { Robolectric.setDefaulthttpResponse(200,""); expect(ConnectivityStatus.isInternetConnected(context)).andReturn(true); therapWebServiceClIEnt.login("","",""); }}
运行测试会产生以下异常:
com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$UnkNownFIEldException: No such fIEld roboguice.activity.RoboActivity.__shadow__---- DeBUGging information ----fIEld : __shadow__class : roboguice.activity.RoboActivityrequired-type : roboguice.activity.RoboActivityconverter-type : com.thoughtworks.xstream.converters.reflection.ReflectionConverterpath : /org.powermock.modules.junit4.rule.PowerMockStatement/outer-class/fNext/fNext/fNext/fTarget/context/__shadow__line number : 16class[1] : net.therap.passreset.service.TherapWebServiceClIEntTestclass[2] : org.junit.internal.runners.statements.InvokeMethodclass[3] : org.junit.internal.runners.statements.RunBeforesclass[4] : org.junit.internal.runners.statements.RunAftersclass[5] : org.powermock.modules.junit4.rule.PowerMockStatementclass[6] : org.powermock.modules.junit4.rule.PowerMockStatementversion : null------------------------------- at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.determineType(AbstractReflectionConverter.java:453) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:294) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234) at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72) at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65) at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallFIEld(AbstractReflectionConverter.java:355) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:306) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234) at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72) at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65) at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallFIEld(AbstractReflectionConverter.java:355) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:306) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234) at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72) at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65) at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallFIEld(AbstractReflectionConverter.java:355) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:306) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234) at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72) at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65) at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallFIEld(AbstractReflectionConverter.java:355) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:306) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234) at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72) at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65) at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallFIEld(AbstractReflectionConverter.java:355) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:306) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234) at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72) at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65) at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallFIEld(AbstractReflectionConverter.java:355) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:306) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234) at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72) at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65) at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66) at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50) at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:134) at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:32) at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1052) at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1036) at com.thoughtworks.xstream.XStream.fromXML(XStream.java:912) at com.thoughtworks.xstream.XStream.fromXML(XStream.java:903) at org.powermock.classloading.DeepCloner.clone(DeepCloner.java:54) at org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:89) at org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:78) at org.powermock.modules.junit4.rule.PowerMockStatement.evaluate(PowerMockRule.java:49) at com.xtremelabs.robolectric.RobolectricTestRunner.evaluate(RobolectricTestRunner.java:292) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263) at org.junit.runners.BlockJUnit4ClassRunner.runchild(BlockJUnit4ClassRunner.java:68) at org.junit.runners.BlockJUnit4ClassRunner.runchild(BlockJUnit4ClassRunner.java:47) at org.junit.runners.ParentRunner.run(ParentRunner.java:231) at org.junit.runners.ParentRunner.schedule(ParentRunner.java:60) at org.junit.runners.ParentRunner.runchildren(ParentRunner.java:229) at org.junit.runners.ParentRunner.accessjava.lang.NullPointerException at roboguice.RoboGuice.setBaseApplicationInjector(RoboGuice.java:116) at net.therap.passreset.service.TherapWebServiceClIEntTest.setUp(TherapWebServiceClIEntTest.java:33) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:45) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30) at org.powermock.modules.junit4.rule.PowerMockStatement.run(PowerMockRule.java:52) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at org.powermock.reflect.internal.WhiteBoxImpl.performMethodInvocation(WhiteBoxImpl.java:2014) at org.powermock.reflect.internal.WhiteBoxImpl.doInvokeMethod(WhiteBoxImpl.java:885) at org.powermock.reflect.internal.WhiteBoxImpl.invokeMethod(WhiteBoxImpl.java:713) at org.powermock.reflect.WhiteBox.invokeMethod(WhiteBox.java:401) at org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:98) at org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:78) at org.powermock.modules.junit4.rule.PowerMockStatement.evaluate(PowerMockRule.java:49) at com.xtremelabs.robolectric.RobolectricTestRunner.evaluate(RobolectricTestRunner.java:292) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263) at org.junit.runners.BlockJUnit4ClassRunner.runchild(BlockJUnit4ClassRunner.java:68) at org.junit.runners.BlockJUnit4ClassRunner.runchild(BlockJUnit4ClassRunner.java:47) at org.junit.runners.ParentRunner.run(ParentRunner.java:231) at org.junit.runners.ParentRunner.schedule(ParentRunner.java:60) at org.junit.runners.ParentRunner.runchildren(ParentRunner.java:229) at org.junit.runners.ParentRunner.access0(ParentRunner.java:50) at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:222) at org.junit.runners.ParentRunner.run(ParentRunner.java:300) at org.junit.runners.Suite.runchild(Suite.java:128) at org.junit.runners.Suite.runchild(Suite.java:24) at org.junit.runners.ParentRunner.run(ParentRunner.java:231) at org.junit.runners.ParentRunner.schedule(ParentRunner.java:60) at org.junit.runners.ParentRunner.runchildren(ParentRunner.java:229) at org.junit.runners.ParentRunner.access0(ParentRunner.java:50) at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:222) at org.junit.runners.ParentRunner.run(ParentRunner.java:300) at org.junit.runner.JUnitCore.run(JUnitCore.java:157) at com.intellij.junit4.JUnit4IDeaTestRunner.startRunnerWithArgs(JUnit4IDeaTestRunner.java:76) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)Process finished with exit code 255<dependency> <groupID>org.powermock</groupID> <artifactID>powermock-module-junit4</artifactID> <version>${powermock.version}</version> <scope>test</scope></dependency><dependency> <groupID>org.powermock</groupID> <artifactID>powermock-module-junit4-rule</artifactID> <version>${powermock.version}</version> <scope>test</scope></dependency><dependency> <groupID>org.powermock</groupID> <artifactID>powermock-API-mockito</artifactID> <version>${powermock.version}</version> <scope>test</scope></dependency><dependency> <groupID>org.powermock</groupID> <artifactID>powermock-classloading-xstream</artifactID> <version>${powermock.version}</version> <scope>test</scope></dependency>0(ParentRunner.java:50) at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:222) at org.junit.runners.ParentRunner.run(ParentRunner.java:300) at org.junit.runners.Suite.runchild(Suite.java:128) at org.junit.runners.Suite.runchild(Suite.java:24) at org.junit.runners.ParentRunner.run(ParentRunner.java:231) at org.junit.runners.ParentRunner.schedule(ParentRunner.java:60) at org.junit.runners.ParentRunner.runchildren(ParentRunner.java:229) at org.junit.runners.ParentRunner.access@RunWith(RobolectricTestRunner.class)@PowerMockIgnore({ "org.mockito.*","org.robolectric.*","androID.*" })@PrepareForTest(Static.class)public class MyTest { @Rule public PowerMockRule rule = new PowerMockRule(); private MyActivity activity; @Before public voID setup() { activity = Robolectric.buildActivity(MyActivity.class).create().get(); } @Test public voID test() throws Exception { powermockito.mockStatic(Static.class); Mockito.when(Static.getCurrentTime()).thenReturn(1); Assert.assertEquals(1,activity.getID()); }}0(ParentRunner.java:50) at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:222) at org.junit.runners.ParentRunner.run(ParentRunner.java:300) at org.junit.runner.JUnitCore.run(JUnitCore.java:157) at com.intellij.junit4.JUnit4IDeaTestRunner.startRunnerWithArgs(JUnit4IDeaTestRunner.java:76) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)Process finished with exit code 255
如果我从类中删除了context字段并将其替换为代码中其他位置的新RoboActivity(),则抛出以下异常:
上述异常似乎表明Robolectric无法创建应用程序上下文.关于如何让两者一起工作的任何想法?
解决方法 我找到了一种将PowerMock与Robolectric结合使用的方法.除了标准的PowerMock jar之外,还需要PowerMock Junit规则.它描述了here如何抓住它.我使用了xstream类加载版本,因为对象是非常错误的.这与PowerMock 1.5.5和Robolectric 2.3一起使用,我不能谈论旧版本.另请注意,不应包含Java代理,因为根据我的经验,它会导致问题.
因此,如果您使用maven,则应声明这些依赖项:
然后你必须像这样设置你的测试类:
总结以上是内存溢出为你收集整理的android – 不能将PowerMock与Robolectric一起使用全部内容,希望文章能够帮你解决android – 不能将PowerMock与Robolectric一起使用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)