1 examples of @Test in JUnit: 1.1 before annotations (@Test)
Private Boolean Istestmethod (Method m) { return = = 0 && m.getname (). StartsWith ( "test") && m.getreturntype (). Equals (Void.type); }
Before using annotations (prior to JUNIT4), JUnit generally obtains a test method in a test class by similar code with the above, obtains the method object through reflection, and then judges that the logic is essentially to see a test method should be: start with Test , no parameters, no return value.
1.2 After using annotations (@Test)
@Test Public void throws Exception {}
Here the main role of @test is to mark testname This method for a test method , the role of labeling, can be regarded as a marker annotation. As with the above, the method labeled for @test is public, parameterless, and has no return value, and a compilation error occurs if the two rules are violated.
1.2.1 Getting the test method flow
- First, get the class object for the classes you want to test
- You can then get an array of methods corresponding to all of the public method.
- Traverse method object to get each method object
- By calling the isannotationpresent (Test.class) method, you can check whether the method is named Test annotation,
- If this annotation is available, the method object's invoke () method is called to execute the method.
for (Method Method:foo. class . Getdeclaredmethods ()) { if (Method.isannotationpresent (org.junit.Test). Class) { System.out.println ("method" + Method + "has junit @Test annotation.") ); }}
definition of 1.2.2 @Test:
@Retention (retentionpolicy.runtime) @Target ({Elementtype.method}) Public@InterfaceTest {/*** Default Empty exception*/ Static classNoneextendsThrowable {Private Static Final LongSerialversionuid = 1L; PrivateNone () {}}/*** Optionally specify <CODE>EXPECTED</CODE>, a throwable, to cause a test method to succeed IFF * An exception of the specified class was thrown by the method. */Class<?extendsThrowable> expected ()defaultNone.class; /*** Optionally specify <code>timeout</code> in milliseconds to cause a test method to fail if it * Takes longer than that number of milliseconds. */ LongTimeout ()default0L;}
Ignore the other details first, @interface, like class, Enum, interface, etc. in Java. This keyword declaration implies a message that it inherits the Java.lang.annotation.Annotation interface and does not declare a interface.
Two members were declared inside:
- Expected, used to verify that the test method throws the expected exception
- Timeout, which verifies whether the test method can run out within a specified time.
Meta annotations
Java meta annotations are used to modify annotations, which may be used when customizing annotations. There are roughly the following types:
- @Document represents this annotation or appears in the document that corresponds to the destination where it is used.
- @Retention Retention Policies
- @Target annotations Apply to target
- @Inherited By default a note is used somewhere in the parent class, and it is not inherited to the subclass, and if you add @inherited to the annotation, it is inherited to the child class.
annotation retention Policies
As the above @Retention (retentionpolicy.runtime) indicates that the annotation is retained at run time, where @Retention is a meta-annotation, the role of the meta-annotation is to be responsible for annotating other annotations. @Retention control the retention policy for this annotation here, the optional values are:
- Sourc Valid in source code
- Class: Valid in class file (i.e. class reserved, default )
- Runtime: Valid at run time (that is, runtime retention)
Application scope of annotations
The @Target ({Elementtype.method}) indicates that the annotation applies to the method, and the optional values are:
- TYPE,/** Field declaration (includes enum constants) */
- FIELD, /** Method declaration * /
- METHOD, /** Parameter declaration * /
- PARAMETER, /** Constructor declaration * *
- CONSTRUCTOR, /** Local Variable declaration * /
- local_variable, /** Annotation type declaration * /
- Annotation_type, /** Package declaration * /
- Package
Annotation effect
- Generate the document. This is the most common, and also the earliest annotations that Java provides. Commonly used @see @param @return, etc.
- Tracking code dependencies, implementing alternative profile features. The most common is the annotation-based configuration that starts with spring 2.5. The function is to reduce the configuration. Now the framework basically uses this configuration to reduce the number of configuration files.
- A format check is made at compile time. such as @override before the method, if you do not override the method of the superclass method, compile time can be checked out.
Other basic built-in annotations
@Override Annotations Enable compile-time checking, you can add the comment to your method to declare that the method is used to overwrite the method in the parent class. If the method is not a method that overrides the parent class, it will be wrong at compile times. For example, we rewrite the ToString () method for a class and write ToString (), and we add a @override comment to the method;
The role of @Deprecated is to add comments to the methods that should not be used, and when programmers use them, they will display a hint at compile time, which has the same function as the @deprecated tag in Javadoc, and, to be exact, It's not as good as Javadoc @deprecated, because it doesn't support parameters,
@SuppressWarnings is different from the first two comments, you need to add a parameter to use correctly, these parameter values are already defined, we use selectively, the parameters are as follows:
Standing on the shoulders of giants, reference materials:
Http://www.blogjava.net/mlh123caoer/archive/2007/09/06/143260.html
Http://www.cnblogs.com/peida/archive/2013/04/24/3036689.html
Http://www.cnblogs.com/mandroid/archive/2011/07/18/2109829.html
Http://www.cnblogs.com/mandroid/archive/2011/07/18/2109829.html
Http://www.cnblogs.com/phoebus0501/archive/2011/02/21/1960077.html