JUnit4.8.2 source code, the most yqj2065 interest is org.junit.runner.Request, now what are the points that mean it?
① Encapsulation of junit inputs
JUNIT4 as the information Processing unit, its input is the unit test class -full of various JUNIT4 runtime labeled classes, but because of the use of reflection mechanism, JUNIT4 input is strictly one or more (group) unit test class object . The earlier version of JUnit mainly deals with a test or test-made tree, and JUNIT4 adds this concept after adding a filter/filtering and sort/sorting support. After all, the direct use of reflection mechanism according to 1.2.4 Java Annotation feed . The code can be very complex.
② isetal father?
Object-oriented. The generic parent does not know its subclasses. But here we see the counter example.
Javadoc generated in the BlueJ
When should the parent class know the subclass ? This is a problem, and I just remember having a knowntype in the data contract in WCF. The request source imports its various subclasses and uses them in a series of static methods to return the request object. Request is an abstract class that returns the object of the request (subclass) through a static method, and how much hatred it has with new. I just like it.
These static methods I don't call them static factories
public static Request AClass (CLASS<?
> Clazz)
public static Request Classwithoutsuitemethod (class<?> clazz)
public static Request classes (computer computer, Class<?> .... classes)
public static Request classes (class<?>...classes)
public static Request runner (final runner runner)
public static Request method (Class<?> clazz, String methodName) {
Description method= description.createtestdescription (clazz,methodname);
Return Request.aclass (Clazz). Filterwith (method);
}
Request.method () encapsulates a @test as a request, after all @Test is the basis. "HelloWorld unit test class TESTINJUNIT4 See source Analysis-1"
Routine 8-3 test request class Package Mytest;import org.junit.runner.*;p ublic class requestdemo{public static void Test () throws exception{ Request rqst = Request.method (Testinjunit4.class, "add"); Result r = new Junitcore (). Run (RQST); System.out.println (R.wassuccessful ());} }
③request used the factory method model .
request has a factory method: Public Abstractrunner Getrunner ();
And the design of the request has a singular place: do you seem to use decorative patterns when creating objects? The request is a detailed component of component, Org.junit.internal.request.ClassRequest in decorative mode, while Filterrequest and Sortingrequest are detailed decorators ( No abstract decorator). And all of this is intended to build the runner associated with the request.
④requestusing Set-string mode
Other non-static methods of request Filterwith, Sortwith can be considered as set methods.
Examples are given in Request.method ():
Return Request.aclass (Clazz). Filterwith (method);
The types covered in this article: request and its 3 subclasses,Description; Design patterns involved: Factory method Mode, set-string mode, decorative mode .
Related types: Runner, Alldefaultpossibilitiesbuilder, filter, and so on.
Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.
JUnit4.8.2 Source Analysis-2 org.junit.runner.Request