NullPointerException 使用 JMockit 测试模拟 java.io.File 两次

发布于 2024-08-20 15:47:27 字数 3007 浏览 6 评论 0原文

当我尝试通过 JMockit 使用模拟文件时,出现 NullPointerException:

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 不太熟悉,我可能只是在做一些愚蠢的事情 - 但我还没有在我使用过的其他模拟对象中看到过这种情况。

有什么想法吗?

I'm getting a NullPointerException when I try to use a mocked File with JMockit:

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();
  }
}

My output is:

 [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] 

The error only happens when testMockedFileCanBeUsed2 is called. If only one test is run, no error is thrown. I see this behavior across test suites - in other words, if I use a mocked File once in a test suite, other test suites using a mocked file will also throw.

Not real familiar with JMockit and it's possible I'm just doing something dumb - but I haven't seen this with other mocked objects I've used.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倦话 2024-08-27 15:47:27

由于 Sun 在类加载机制中引入的优化,JDK 1.6.0_18 中会出现此问题。不久前它已在 JMockit 中修复。

尝试当前的预览版 0.997 版本,它可以工作:
http://code.google.com/p/jmockit/downloads/list

This issue occurs with JDK 1.6.0_18, due to the optimizations that Sun introduced in the class loading mechanism. It was fixed in JMockit a while ago.

Try the current preview release of version 0.997, it will work:
http://code.google.com/p/jmockit/downloads/list

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文