The-java new feature of the mainstream Unit testing tool-annotation

Source: Internet
Author: User
Tags database join

Write leader Liang Weilong

1: What is annotation?

Annotation, the word "@xxx" (such as @Before, @After, @Test (timeout=xxx), @ignore), is generally translated into meta data and is a new feature of Java.

2: A brief introduction to the meta-data:

@Before:
The method that uses the metadata is executed one time before each test method executes.

@After:
The method that uses the metadata is executed once after each test method executes.

Attention:@Beforeand the@Afterthe method of marking can only have one. This is the equivalent of replacingJUnitin previous versions ofsetUpand theTearDownmethod and of course you can continue to call that name, butJUnityou're not going to be so bossy.

@Test (Expected=*.class)
InJUnit4.0before the test of the error, we can only passfailto produce an error, and inTryblock insideasserttrue(true) to test. Now, by@Testin the metadataexpectedproperty. expectedThe value of the property is an exception type

@Test (TIMEOUT=XXX):
The metadata passed in a time (in milliseconds) to the test method,
If the test method does not run out within the time set, the test also fails.

@ignore:
The test method for the metadata token is ignored in the test. Use this tag to mark the method when the test method has not been implemented, or if the method of testing is outdated, or if the method can be tested under certain conditions (for example, a database join is required, and the database is not connected at local testing). At the same time, you can pass aStringparameters to indicate why this test method is ignored. For example:@lgnore ("the method has not yet been implemented"), when executed, only reports that the method is not implemented and does not run the test method.

3: in Java Annotation for unit testing:

There are two methods defined here:

Max (int x,int y); The largest of the two numbers

Min (int x,int y); The smallest of the two numbers

Specific process: (people do as I say, experience experience first)

A: Open Exlipse, set up a project Junit_test (name casually);

B: Click Next, select "Library", B: Click Next, the page selection appears: C: Select "Add Library" on the right, select "JUnit" on the page that appears, D: Click Next, then click Done! E: Then under the current project, build a package, a class, write code: public class Printfannotation {int, a, B, public void max (int x,int y) {int maxnum;if (x<y) ma Xnum=y;else{maxnum=x;} System.out.println (maxnum);} public void min (int x,int y) {int minnum;if (x<y) minnum=x;else{minnum=y;}   System.out.println (Minnum);}} Both: F: On the left side of the class name, right click--〉 New--〉junit test case, the page that appears click Next: G: In the selection of your own code written by the method Max (); min (); Click Finish. H: Code fail in the page that appears ("not implemented"); Delete this code: write to New Printfannotation (). Max (5,6); (Note: I only wrote a method Testmin () method not written) I: Select the method name, right-click--〉 Debug mode --〉junit test, the following results appear: (note, the green bar appears on the left to indicate success, the console appears max (5,6) test results, 64 summary: We understand more than this one test method, but this way requires everyone will! Thank you for pointing out my mistakes and making progress together! about annotation (yuan annotation), we go online more search!

The-java new feature of the mainstream Unit testing tool-annotation

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.