2. Reflection (reflect)
Reflection is a feature that occurs at the beginning of java1.5. Reflection is a method that uses the name of the callback class, method, domain, and Declaration. The appearance of reflection methods adds more features to Java.
A. access private type attributes and Methods
Because you cannot access private domains and methods, you cannot test the corresponding functions. However, reflection provides a way to access the private method. Therefore, xunit testing can be performed on the private method.
1 Public ClassTestee {2 3Private VoidExecute (){4System. Out. println ("method called");5 }6}
1 Import Java. Lang. Reflect. invocationtargetexception; 2 Import Java. Lang. Reflect. method; 3 4 Import Org. JUnit. After; 5 Import Org. JUnit. before; 6 Import Org. JUnit. test; 7 8 Public Class Tester { 9 10 @ Before 11 Public Void Setup () Throws Exception { 12 } 13 14 @ After 15 Public Void Teardown () Throws Exception { 16 } 17 18 @ Test 19 Public Void Test (){ 20 Testee =New Testee (); 21 Try { 22 Method method = testee. Class . Getdeclaredmethod ("execute ", New Class [] {}); 23 Method. setaccessible ( True ); 24 Method. Invoke (testee, Null ); 25 } Catch (Securityexception e ){ 26 E. printstacktrace (); 27 } Catch (Nosuchmethodexception e ){ 28 E. printstacktrace (); 29 } Catch (Illegalargumentexception e ){ 30 E. printstacktrace (); 31 } Catch (Illegalaccessexception e ){ 32 E. printstacktrace (); 33 } Catch (Invocationtargetexception e ){ 34 E. printstacktrace (); 35 } 36 } 37 38 }
The reflection mechanism can help increase the test coverage rate.
B. traverse the domain, declaration, or method
As described in the previous section, in order to verify the domain, we need to traverse all the domains. The examples in the previous section have mentioned the traversal methods for methods, fields, and declarations.
C. generate different types of object instances by name
When using the state pattern or strategy pattern, you can generate a class instance based on the name.
1 Public InterfaceState {2Public VoidRun ();3}
Public ClassAstateImplementsState {@ overridePublic VoidRun () {system. Out. println ("A state");}}
1 Public ClassBstateImplementsState {2 3 @ Override4Public VoidRun (){5System. Out. println ("B state");6 }7}
1 Public Class Statefactory { 2 3 Public Static State create (string name ){ 4 5 Class clazz; 6 Try { 7 Clazz = Class. forname (name + "state" ); 8 Return (State) clazz. newinstance (); 9 } Catch (Classnotfoundexception e ){ 10 E. printstacktrace (); 11 } Catch (Instantiationexception e ){ 12 E. printstacktrace (); 13 } Catch (Illegalaccessexception e ){ 14 E. printstacktrace (); 15 } 16 Return Null ; 17 } 18 19 }
1 Public class starter { 2 3 Public static void main (string [] ARGs) { 4 string name =" A "; 5 state = statefactory. create (name); 6 7 state. run (); 8 } 9 10 }
in this way, the instance of the class can be generated by name. The preceding example shows the State mode. You can think of the Rule mode in turn. For example, a calculator can generate a calculated Class Based on the symbol name. The specific production is left for readers to try.