Unit Testing method which uses class which is not in classpath
I have a method which is used to check if the given class is instance of perticular type. Say for example myClass is an instance of DBClass I want to return true. And the DBClass is dynamically defined by customer in the properties file. So I am reading the properties file, getting the DBClass super type , loading it and checking if given class (i.e myClass) is instance of the DBClass.
And one thing is for sure t开发者_如何学Gohat the DBCLass which i am trying to check for supertype will not be present in my classpath at running the test case as this dbClass is dynamic and customer specific.
I am not sure how to directly test it or cover it.I am not sure if I need to mock properties file?
I am using Junit and JMock.
Any suggestions on testing the method. Method is written something like this.
public boolean isDBClass (final Class<?> myClass) {
//Following line reads the properties file and get the class name for db parent class.
String dbSuperClass = PropertiesReader.PropertyEnum.DB_CLASS_PARENT.toString();
// if myClass is subclass of dbSuperClass return true, false otherwise.
return loadClass(dbSuperClass).isAssignableFrom(myClass);
}
Your method's responsibilities are:
- get a String
- pass it to method called loadClass()
- call isAssignableFrom() with the supplied input parameter
- return the result
If you could mock the loadClass method then you could verify these responsibilities without actually doing any class loading.
You may need to do a little refactoring in order to make loadClass mock-able but that approach would unit this particular method.
Well I have find out a work around currently. I am providing the a dummy properties file which refers to the dummy class which is in my classpath.
精彩评论