Java (5) --- talking about JUnit 4

Source: Internet
Author: User

Java (5) --- talking about JUnit 4

Junit4

JUnit4 fully introduces Annotation to execute the tests we have written.

1. JUnit 4 does not require the test class to inherit the TestCase parent class.

2. In a Test class, all public and void Methods Modified by the @ test annotation are Test cases and can be executed by JUnit.

3. Although JUnit 4 does not require the test method name to start with test, we 'd better start with test as per JUnit 3.8's requirements.

4. In JUnit 4, the @ Before annotation is used to implement the same functions as the setUp method in JUnit3.8, And the @ After annotation is used to implement the same functions as the tearDown method in JUnit 3.8.

5. in JUnit 4, you can use the @ BeforeClass and @ AfterClass annotations to modify a public static void no-arg method, in this way, the method modified by the @ BeforeClass annotation is executed before all test methods are executed. The method modified by the @ AfterClass annotation is executed after all test methods are executed.

6. @ Ignore annotation can be used to modify the test class and test method. When the test class is modified, all test methods in the class are ignored. When the test method is modified, the test method is ignored.

7. Parameters)

When a test class runs with a Parameterized runner, @ RunWith (Parameterized. class) annotation, indicating that this class will run without using the built-in runner of JUnit, but with the parameterized runner; the @ Parameters annotation should be used to modify the methods that provide Parameters in the parameterized running class, and assign values to each parameter in the constructor of the test class (the constructor calls the constructor ), finally, write the test class, which runs the test multiple times according to the number of parameter groups.

8. In JUnit 4, if you want to run multiple tests at the same time, you need to use two Annotations:

@ RunWith (Suite. class) and @ Suite. suiteClasses () specifies the use of the Suite runner to run the test, and specify the test classes to run. In the @ SuiteClasses clause, you can specify the Suite, in this way, JUnit will look for the Test classes and find and execute the Test cases that can be executed.

9. Two methods can be used to test the private method of the class:

1) modify the access modifier of the method and change private to default or public (but this method is not recommended ).

2) use reflection to call the private method of the target class in the test class (recommended ).

10. TestSuite: Multiple tests can be combined to execute multiple tests at the same time.

11. Identify error and failure in JUnit)

1) An error occurs when an exception is thrown in the Code and affects normal code execution. For example, an ArrayIndexOutOfBoundsException is thrown, which is called an error.

2) failure refers to the inconsistency between the expected result and the actual execution result of the program, or the direct call of the fail () method. This is called failure.

12. Observer mode used in JUnit

Related Article

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.