1. JUnit applies the template method mode in testcase:
Public void runbare () throws throwable {
Setup ();
Try {
Runtest ();
} Finally {
Teardown ();
}
}
2. JUnit applies the adapter mode in the testcase class:
Public void runbare () throws throwable {
Throwable exception = NULL;
Setup ();
Try {
Runtest ();
} Catch (throwable running ){
Exception = running;
} Finally {
Try {
Teardown ();
} Catch (throwable tearingdown ){
If (exception = NULL)
Exception = tearingdown;
}
}
If (exception = NULL) return;
Throw exception;
}
In the runbare () method, we use the runtest () method to adapt our own testxxx () method, so that JUnit can execute our own testcase. The runtest method is implemented as follows:
Protected void runtest () throws throwable {
Assertnotnull (this. fname );
Method runmethod = NULL;
Try {
Runmethod = super. getclass (). getmethod (this. fname, (class []) null );
} Catch (nosuchmethodexception e ){
Fail ("Method \" "+ this. fname +" \ "not found ");
}
If (! (Modifier. ispublic (runmethod. getmodifiers ()))){
("Method \" "+ this. fname +" \ "shocould be public ");
}
Try {
Runmethod. Invoke (this, (object []) new class [0]);
} Catch (invocationtargetexception e ){
E. fillinstacktrace ();
Throw E. gettargetexception ();
} Catch (illegalaccessexception e ){
E. fillinstacktrace ();
Throw E;
}
}
3. Observer Mode
/**
* A listener for Test Progress
*/
Public interface testlistener {
/**
* An error occurred.
*/
Public void adderror (test, throwable t );
/**
* A failure occurred.
*/
Public void addfailure (test, assertionfailederror t );
/**
* A test ended.
*/
Public void endtest (test );
/**
* A test started.
*/
Public void starttest (test );
}
4. Command)
After command is used, the architecture effect for the system is as follows:
In command mode, the Requested Party (testcase development) and the called party (JUnit) are decoupled.
The command mode makes the new testcase easy to add. You only need to inherit the testcase class without changing the existing class.
In command mode, multiple testcase can be combined into a composite command. testsuite is a composite command. Of course, it uses the composite mode.
In command mode, testcase, which is vulnerable to reverse requests, is combined into a request queue, so that the party receiving the request (JUnit framwork) can easily decide whether to execute the request, once a test failure or error is found, you can stop the report.
5. decoration Mode
6. Composite)