Having heard so much about Test Driven Development, I was wondering how easily and effectively I could incorporate this into Vajra. I am currently in the process of writing the native methods for the few classes that the JVM author needs to implement in order to integrate the JVM with Classpath. Each such class has an associated .so file that contains the native methods for that class. If I were to write a unit test for these native methods, let's say for the class java.lang.reflect.Constructor, and more specifically for the method getDeclaringClass(), what would I need?
The native method definition looks something like this:
JNIEXPORT jclass JNICALL Java_Constructor_getDeclaringClass(JNIEnv *env, jobject obj) ;
In order to unit test this method from a harness, I should have these things available:
1) A functional JNIEnv pointer
2) A jobject
Both of these structures are either direclty mapped to internal classes in Vajra, or are members of these classes. If I need to correctly initialise these structures, those internal classes will have to be initialised first. This brings into play the entire internal object hierarchy of Vajra, with all its dependencies, some of which border on the incestuous.
There are two ways I can proceed here:
(a) invoke the virtual machine on a sample class that in turn makes the call to getDeclaringClass() in its main method, thereby invoking the native method
(b) create stubs for all the internal objects I need to be initialised before I invoke getDeclaringClass() from the harness.
Option (a) is the easier option for me, but is more of an integration test. Option (b), while technically the correct way to do unit testing, would involve a humongous effort at setting up the testing environment.
I do not know whether the 'failure' (if I can call it that) of the unit test approach in this instance is due to the inherent complexity of the problem or because I am going about this in the wrong manner. I suspect that the answer lies somewhere in between. It could also be that the design deficiencies in Vajra (a lack of testability) play a part in this.