Jtest API

jtest
Class Stubs

java.lang.Object
  |
  +--jtest.Stubs

public abstract class Stubs
extends java.lang.Object

Base class for all Stubs Classes.

User Defined Stubs are specified by writing special Java classes called "Stubs Classes". Stubs Classes should extends this class and should use the utility methods/fields provided in this class.


Field Summary
protected static java.lang.Object NO_STUB_GENERATED
          Object used to specify that a stubs() method doesn't generate a stub.
protected static java.lang.Object VOID
          Object used to specify that a stubs() method returns a void type.
 
Method Summary
protected static java.lang.Object makeStubObject(java.lang.Class cl)
          Creates a stub object for the class cl.
static java.lang.Object stubs(java.lang.reflect.Method method, java.lang.Object _this, java.lang.Object[] args, java.lang.reflect.Method caller_method, boolean executing_automatic)
          Stubs generator method.

The implementator of this class should write a stubs method with this signature.

Whenever a method external to the class is invoked in the class under test, Jtest will call this stubs() method.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NO_STUB_GENERATED

protected static final java.lang.Object NO_STUB_GENERATED
Object used to specify that a stubs() method doesn't generate a stub.

VOID

protected static final java.lang.Object VOID
Object used to specify that a stubs() method returns a void type.
Method Detail

makeStubObject

protected static java.lang.Object makeStubObject(java.lang.Class cl)
Creates a stub object for the class cl. Stub objects are very useful when writting user defined stubs. A stub object is like any other object, with the following differences:

a) The stub object can be an instance of an interface. I.e. the following creates an instance of "Enumeration":
         Enumeration enum = (Enumeration) makeStubObject (Enumeration.class);
 
b) Any method invocation that uses a stub object (i.e. for the "this" object or for any of its arguments) is an stub even if no stub has been defined for that method invocation. If no stub has been defined for it a default stub returning the default initialization value for the method return type is used (i.e. null for Object, 0.0d for double, ...).

c) If an stub object is used in a contructor invocation, then the constructed object is also treated as a stub object.

Stub objects are necessary to be able to test classes that use interfaces for which an implementation has not been written yet. They can be used whenever an object of the interface class needs to be created. They can also be used whenever one wants to create an object of a given type without having to call any specific constructor (i.e. instead of using new java.io.FileInputStream ("what to put here?)", one can use:
(FileInputStream) JT.makeStubObject (java.io.FileInputStream.class)
this creates a FileInputStream object, but no constructor is called to initialize it.

stubs

public static java.lang.Object stubs(java.lang.reflect.Method method,
                                     java.lang.Object _this,
                                     java.lang.Object[] args,
                                     java.lang.reflect.Method caller_method,
                                     boolean executing_automatic)
Stubs generator method.

The implementator of this class should write a stubs method with this signature.

Whenever a method external to the class is invoked in the class under test, Jtest will call this stubs() method. The stubs() method should decide if a stub should be used for that method or not. If no stub should be used, the method should return NO_STUBS_GENERATED. If the method return type is void, the stubs() method should return VOID.

If a stub should be used for an invocation, stubs() should either, return the value for the stub, or throw the exception that the stub should throw. The return value should be wrapped in an Object if it is a primitive type. For example if stubs() decides that a given external call should return the integer 3, the value returned by stubs() should be: new Integer (3).

To define stubs for constructor invocations, define a stubs() method with a first parameter of type Constructor instead of Method.

If some stubs() method is not defined, no stubs will be used for those members (i.e. Method or Constructor).

When defining stubs() methods, all the parameters except the first are optional. For example one can define a stubs() method of the form: Object stubs (Method method) instead of the longer version.

Stubs are only used for references from within the tested class.

Parameters:
method - external method being invoked.
_this - this Object if an instance method, null otherwise.
args - arguments to the method invocation.
caller_method - method calling method.
executing_automatic - true if executing an automatic Test Case.

Jtest API