-java new features of the mainstream Unit testing tool-annotation Writer: group leader Liang Weilong

Source: Internet
Author: User
Tags database join

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.

-java new features of the mainstream unit testing Tools-annotation-groupthreetogether-group Blog

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.

Note: @Before and @after can only be marked by one method. This is equivalent to replacing the setup and Teardown methods in the previous version of JUnit, but you can still call that name, but JUnit will not ask you to do so.

@Test (Expected=*.class)
Before JUnit4.0, for the test of the error, we can only generate an error by fail, and test it in the try block Asserttrue (true). Now, by @test the expected property in the metadata. The value of the expected 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 a string argument to the label to indicate why this test method is ignored. For example: @lgnore ("The method has not been implemented"), when executed, only reports that the method is not implemented and does not run the test method.

3: Unit Test in Java with annotation:

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);

-java new features of the mainstream unit testing Tools-annotation-groupthreetogether-group Blog
B: Click Next, select "Library";
-java new features of the mainstream Unit testing tool-annotation-groupthreetogether-group Blog B: Click Next to the page selection that appears:
C: Select "Add Library" on the right, select "JUnit" on the page that appears;
-java new features of the mainstream unit testing Tools-annotation-groupthreetogether-group Blog
D: Click Next, then click Done!
-java new features of the mainstream unit testing Tools-annotation-groupthreetogether-group Blog
E: Then under the current project, build a package, a class, and write the code as follows:
public class Printfannotation {

int A, B;

public void max (int x,int y) {
int maxnum;
if (x<y)
Maxnum=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
-java new features of the mainstream unit testing Tools-annotation-groupthreetogether-group Blog
F: On the left side of the class name, right click--〉 New--〉junit test case, the page that appears click Next:
-java new features of the mainstream unit testing Tools-annotation-groupthreetogether-group Blog
G: In choosing your own code to write the method Max (); min (); Click Finish.
-java new features of the mainstream unit testing Tools-annotation-groupthreetogether-group Blog
H: Code fail ("not implemented") in the page that appears, remove this code: write to New Printfannotation (). Max (5,6);
(Note: I have only written a method Testmin () method not written)
-java new features of the mainstream unit testing Tools-annotation-groupthreetogether-group Blog
I: Choose the method Name, right click--〉 Debug Mode--〉junit test, the following results appear:
-java new features of the mainstream unit testing Tools-annotation-groupthreetogether-group Blog
(Note that the green bar appears on the left to indicate success, the console appears max (5,6) test results, 6 mainstream Unit testing tool-java new features-annotation-groupthreetogether-group Blog
4 Summary:
We know more than this kind of 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!

-java new features of the mainstream unit testing tools-annotation write leader Liang Weilong

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.