JUnit common annotations

Source: Internet
Author: User
Java annotation (annotation) is used by @ annotation name, which can be implemented by simple words. Commonly used annotations in JUnit include @ test, @ ignore, @ beforeclass, @ afterclass, @ before, @ After, @ runwith, and @ parameters. The following are related introductions and instructions: 1. [email protected] In junit3, the test class and test method are named to determine whether it is a test, and all test classes must inherit the test base class of JUnit. In junit4, defining a test method is much easier. Just add @ test to the method. Note: The test method must be public void, that is, public and no data is returned. You can throw an exception. 2. [email protected] Sometimes we want to temporarily not run some test methods \ test classes. You can add this annotation before the methods. In the running result, JUnit calculates the number of ignored use cases to remind you. However, we do not recommend that you do this frequently, because it is easy to forget to update these test methods, resulting in insufficient code and missing use cases. 3. [email protected] When we run several associated use cases, we may execute the same commands in data preparation or other preliminary preparations to make the code clearer, less redundant, you can extract the public part, put it in a method, and annotate @ beforeclass for this method. This method is run once before all the test cases in the test class are run. For example, creating database connections and reading files. Note: The method name can be arbitrary, but must be public static void, that is, public, static, and no return. This method runs only once. 4. [email protected] corresponds to @ beforeclass and runs once after all the use cases in the test class are run. It is used to process subsequent testing tasks, such as cleaning data and restoring the site. Note: it must also be public static void, that is, public, static, and no return. This method runs only once. 5. The difference between [email protected] And @ beforeclass is that @ before runs more than once and runs once before each use case is run. It is mainly used for preparations independent of use cases. For example, both use cases need to read user a information in the database, but the first use case deletes User A, and the second use case needs to modify user. You can use @ beforeclass to create a database connection. Use @ before to insert a user. Note: It must be public void and cannot be static. Run more than once, depending on the number of use cases. 6. [email protected] corresponds to @ before. 7. [email protected] First, we need to clarify several concepts: test methods, test classes, test sets, and test runners. The test method is some functions annotated with @ test. A test class is a ** test. Java file that contains one or more test methods. A test set is a suite and may contain multiple test classes. The test runner determines how the tester prefers to run these test sets, classes, and methods. @ Runwith is placed before the test class name to determine how the class runs. You can also use the default runner without labeling. Common runners: 1. @ runwith (parameterized. class) parameterized runner, used with @ parameters to use the Parameter Function of JUnit [email protected] (suite. class) @ suiteclasses ({atest. class, btest. class, ctest. class}) the test set runner works with the [email protected] (junit4.class) junit4 default runtime [email protected] (junit38classrunner. class) is used for the runner compatible with junit3.8. some other runners have more functions. For example, @ runwith (springjunit4classrunner. Class) integrates some spring functions 8 [email protected]

JUnit common annotations

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.