运行PowerMock测试用例给出了RuntimeException:Stub

时间:2019-03-06 14:02:29

标签: powermockito powermockrunner

我正在修改一些测试用例以开始使用PowerMockito。下面是我编写的示例测试类

@PrepareForTest({Setup.class})
public class TestSampleUtil {
    @Rule
    public PowerMockRule rule = new PowerMockRule();

    @Mock
    Setup fakeSetupClass;

    @Before
    public void init() {
        fakeSetupClass = Mockito.mock(Setup.class);
        PowerMockito.mockStatic(Setup.class);
        PowerMockito.when(Setup.getInstance()).thenReturn(fakeSetupClass);
    }

    @Test
    public void SampleUtil_defaultValues()
    {
        PowerMockito.when(Setup.getInstance().readIntConf(10)).thenReturn(1);
        SampleUtil util = new SampleUtil(RuntimeEnvironment.application.getApplicationContext());
        SampleUtilConfig sampleUtilConfig = util.getSampleConfigTest();
        Assert.assertEquals(sampleUtilConfig.getSampleDataModel().getAllowFeature(), true);
    }

但是每次我运行测试用例时,都会不断出现以下异常:

java.lang.RuntimeException: Stub!

    at org.xmlpull.v1.XmlPullParserFactory.newInstance(XmlPullParserFactory.java:117)
    at com.thoughtworks.xstream.io.xml.XppDriver.createParser(XppDriver.java:57)
    at com.thoughtworks.xstream.io.xml.AbstractXppDriver.createReader(AbstractXppDriver.java:54)
    at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1085)
    at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1076)
    at org.powermock.classloading.DeepCloner.clone(DeepCloner.java:55)
    at org.powermock.classloading.AbstractClassloaderExecutor.executeWithClassLoader(AbstractClassloaderExecutor.java:56)
    at org.powermock.classloading.SingleClassloaderExecutor.execute(SingleClassloaderExecutor.java:67)
    at org.powermock.classloading.AbstractClassloaderExecutor.execute(AbstractClassloaderExecutor.java:43)
    at org.powermock.modules.junit4.rule.PowerMockStatement.evaluate(PowerMockRule.java:75)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)

有人可以帮助我找出可能出了什么问题吗?谢谢。

0 个答案:

没有答案