views:

83

answers:

2

I am testing my code with a negative test case i.e I have removed one of the keys from the property file. In this case the code is suppose to give a missing resource message(Looger msg given in the catch block of Missing Resource excpetion),but instead it is giving a message from IO exception's catch block and the message is "IO Exception : java.lang.NullPointerException".The code flow is like this: Main class has the method which is trying to get the key from a constants file constants file inturn gets the value from a property file using resource bundle. I am creating an instance of the constants file in my main class.This instance is coming null,when I remove the key from the property file.

A: 

Accessing a key that no longer exists already exists in Java as an IOException, so that will get thrown regardless. If you then want to throw a MissingResourceException you will have to do it within the IOException catch block.

Alternatively, you can check the key for null, and if it is, throw a MissingResourceException.

AlbertoPL
+1  A: 

The Properties class returns null when the key is missing, so you likely have code that does something like this:

} catch (Exception e) {
   throw new IOException(e);
}

And that exception is a null pointer exception when you try to use the result. Instead you have to check for null and throw the missing resource exception if that is what the class returns.

Yishai