PowerMock - 模拟静态系统类抛出 IllegalStateException



>我有以下代码

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 阅读了这里的文档,但测试不起作用。我错过了什么吗?

编辑:我用一个真正的A对象测试了以前的代码(并将其从重播中删除)

  classUnderTest = new A();

但我仍然得到同样的例外。

尝试将 File.class 添加到@PrepareForTest。

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

问候阿尼尔·夏尔马

添加文件.class 到@PrepareForTest并检查重播和验证的导入。它们应该来自PowerMock。

我认为您需要抑制构造函数

检查这个http://code.google.com/p/powermock/wiki/SuppressUnwantedBehavior

相关内容

  • 没有找到相关文章

最新更新