PowerMock - 惩戒静态系统类抛出IllegalStateException异常(Powe

2019-09-18 05:01发布

我有以下代码

public class A{    
    public void createFile() {
        File tempXmlFile = null;
        String extension = ".xml";
        String name = "someName";
        try {
            tempXmlFile = File.createTempFile(name, extension);

            if (tempXmlFile.exists()) {
                tempXmlFile.delete();
            }
        } catch (IOException e) {
            System.out.println(e.getStackTrace());
        }
    }
}


@RunWith(PowerMockRunner.class)
@PrepareForTest(A.class)
public class testA  extends TestCase{
    private A classUnderTest;

    @Override
    @Before
    public void setUp() {
        classUnderTest = PowerMock.createMock(A.class); //the class is more complex in my case and I have to mock it
    }

    public void testCreateFile() throws IOException{
        String extension = ".xml";
        String name = "someName";
        PowerMock.mockStatic(File.class);
        File tempFileMock = PowerMock.createMock(File.class);

        expect(File.createTempFile(name, extension)).andReturn(tempFileMock);
        expect(tempFileMock.exists()).andReturn(true);
        expect(tempFileMock.delete()).andReturn(true);

        replay(File.class, tempFileMock, classUnderTest);
        classUnderTest.createFile();
        verify(File.class, tempFileMock, classUnderTest);
    }
}

在测试类,我说下测试的类必须被嘲笑(我不能创建新的对象)。 当我运行测试,我得到:

java.lang.IllegalStateException: no last call on a mock available
    at org.easymock.EasyMock.getControlForLastCall(EasyMock.java:174)
    at org.easymock.EasyMock.expect(EasyMock.java:156)
    at myPackage.testA.testCreateFile(testA.java:35)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:59)
    at org.junit.internal.runners.MethodRoadie.runTestMethod(MethodRoadie.java:98)
    at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:79)
    at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:87)
    at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:77)
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:42)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:163)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:120)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:113)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:111)
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:87)
    at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:44)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

我在这里阅读文档http://code.google.com/p/powermock/wiki/MockSystem但测试STIL wont't工作。 我缺少的东西吗?

编辑:我测试与真实的目的前面的代码(和从重放删除它)

  classUnderTest = new A();

但我仍然得到同样的异常。

Answer 1:

尝试添加File.class到@PrepareForTest。



Answer 2:

尝试org.powermock.reflect.internal.WhiteboxImpl.newInstance()方法来创建的类的对象,然后直接调用方法。 1),这将抑制类2)如果你的类包含静态块,然后用suppressStaticInitilaizationFor来抑制的构造。

对于阿尼尔·夏尔马



Answer 3:

添加File.class到@PrepareForTest和检查重播的进口和验证。 它们应该是PowerMock。



Answer 4:

我认为你需要抑制构造

勾选此http://code.google.com/p/powermock/wiki/SuppressUnwantedBehavior



文章来源: PowerMock - Mocking static system class throws IllegalStateException