5

I have a class that extends from ServiceTestCase to test my Android Service implementation.

public class MainCoreServiceTest extends ServiceTestCase<CoreService> {

    protected static final String DB_PATH = "/data/data/"
            + CoreService.class.getPackage().getName() + "/databases/"
            + DatabaseManager.DB_NAME;

    // More code
}

The problem I am facing is that if I run only one test method, everything is fine, but if I launch the whole class (which contains several test methods) then on second test method I get an ExceptionInInitializerError and I found out this is because of DB_PATH being null. This is the method where this happens:

private void wipeOutDB() {

    // Erase DB file
    File dbFile = new File(DB_PATH);
    if (dbFile.exists()) {
        assertTrue(dbFile.delete());
    }

    // Erase journal file
    dbFile = new File(DB_PATH + "-journal");
    if (dbFile.exists()) {
        assertTrue(dbFile.delete());
    }
}

new File(DB_PATH) obviously fails when DB_PATH is null

I don't modify this constant anywhere (anyway I can't modify it because it's final), so I don't understand this behavior.

If I move this constant to another class/interface, it works fine.

Can anyone please explain this behavior? Thanks in advance!

m0skit0
  • 25,268
  • 11
  • 79
  • 127
  • `wipeOutDB()` would fail with a `NullPointerException` inside the File constructor. Maybe you have some [circular static dependency](http://stackoverflow.com/questions/6416408/static-circular-dependency-in-java) that leads to an [unexpected exception in a static initializer](http://docs.oracle.com/javase/6/docs/api/java/lang/ExceptionInInitializerError.html) – zapl Nov 23 '12 at 14:54
  • Yes it does fail with an NPE, but why only on second test? Makes no sense... – m0skit0 Nov 26 '12 at 12:10

1 Answers1

2

The Exception you get means, that DB_PATH couldn't be initialized - thus it is null later on. I guess one of the parts you use to init DB_PATH is null, probably somewhere in this one: CoreService.class.getPackage().getName().

Also see: http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/ExceptionInInitializerError.html

Ridcully
  • 23,362
  • 7
  • 71
  • 86
  • How could `CoreService.class.getPackage().getName()` have anything null? AFAIK this is fully static information. And it works on first test... – m0skit0 Nov 26 '12 at 12:11