tags:

views:

263

answers:

1

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?

+1  A: 

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

Rogerio