Design patterns used in JUnit

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.