0
我得到一個NullPointerException當我嘗試使用帶有JMockit一個嘲笑文件:NullPointerException異常使用JMockit來測試嘲笑java.io.File的兩次
package com.metamolecular.poobah.test;
import java.io.File;
import junit.framework.TestCase;
import mockit.Expectations;
import mockit.Mocked;
public class MockedFileTest extends TestCase
{
@Mocked File file;
public void testMockedFileCanBeUsed1() throws Exception
{
new Expectations()
{
{
file.list();
result = null;
}
};
file.list();
}
public void testMockedFileCanBeUsed2() throws Exception
{
new Expectations()
{
{
file.list();
result = null;
}
};
file.list();
}
}
我的輸出是:
[java] 1) testMockedFileCanBeUsed2(com.metamolecular.poobah.test.MockedFileTest)java.lang.NullPointerException
[java] at java.io.FilePermission.init(FilePermission.java:193)
[java] at java.io.FilePermission.<init>(FilePermission.java:249)
[java] at sun.net.www.protocol.file.FileURLConnection.getPermission(FileURLConnection.java:198)
[java] at sun.net.www.protocol.jar.JarFileFactory.getPermission(JarFileFactory.java:124)
[java] at sun.net.www.protocol.jar.JarFileFactory.getCachedJarFile(JarFileFactory.java:94)
[java] at sun.net.www.protocol.jar.JarFileFactory.get(JarFileFactory.java:50)
[java] at sun.net.www.protocol.jar.JarURLConnection.connect(JarURLConnection.java:104)
[java] at sun.net.www.protocol.jar.JarURLConnection.getInputStream(JarURLConnection.java:132)
[java] at java.net.URL.openStream(URL.java:1010)
[java] at java.lang.ClassLoader.getResourceAsStream(ClassLoader.java:1232)
[java] at mockit.internal.ClassFile.readClass(ClassFile.java:55)
[java] at mockit.internal.ClassFile.createClassFileReader(ClassFile.java:44)
[java] at mockit.internal.ClassFile.<init>(ClassFile.java:72)
[java] at mockit.internal.RedefinitionEngine.restoreOriginalDefinition(RedefinitionEngine.java:316)
[java] at mockit.internal.state.MockFixture.restoreAndRemoveRedefinedClasses(MockFixture.java:174)
[java] at mockit.internal.state.SavePoint.restoreClassesRedefinedAfterSavepoint(SavePoint.java:77)
[java] at mockit.internal.state.SavePoint.rollback(SavePoint.java:57)
[java] at mockit.internal.state.SavePoint.rollbackForTestClass(SavePoint.java:89)
[java] at mockit.integration.TestRunnerDecorator.updateTestClassState(TestRunnerDecorator.java:54)
[java] at mockit.integration.junit3.internal.JUnitTestCaseDecorator.runBare(JUnitTestCaseDecorator.java:81)
[java] at com.metamolecular.poobah.test.Main.main(Main.java:45)
[java]
[java] FAILURES!!!
[java] Tests run: 2, Failures: 0, Errors: 1
[java]
錯誤只發生在調用testMockedFileCanBeUsed2時。如果只運行一個測試,則不會引發錯誤。我在測試套件中看到了這種行爲 - 換句話說,如果我在測試套件中使用過一次模擬文件,那麼使用模擬文件的其他測試套件也會丟失。
對JMockit不太熟悉,而且可能我只是在做一些愚蠢的事情 - 但我還沒有看到過我用過的其他模仿對象。
任何想法?