Java Learning Chapter (v)---on JUnit 4

Source: Internet
Author: User

Junit4

JUNIT4 fully introduced the Annotation to perform the tests we wrote.

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

2. In a test class, all public,void methods that are modified by the @test annotations are test case and can be executed by JUnit.

3. Although JUnit 4 does not require the test method name to start with the tests, it is best to use test as the beginning of the test method name as required by JUnit 3.8.

4. In JUnit 4, the same functionality as the Setup method in JUnit3.8 is implemented through the @before annotations, with the same functionality as the TearDown method in JUnit 3.8 through the @after annotations.

5. In JUnit 4, you can use the @beforeclass and @afterclass annotations to decorate a public static void No-arg method so that the method modified by the @beforeclass annotation executes before all test methods are executed; The methods that are modified by @AfterClass annotations are executed after all test methods have been executed.

6. @Ignore annotations can be used to modify test classes and test methods, which means that all test methods in the class are ignored when the test class is decorated, and that the test method is ignored when the test method is decorated.

7. Parametric testing (Parameters)

When a test class is run with a parameterized runner, the @runwith (parameterized.class) annotation is added to the class's declaration, indicating that the class will not run with the JUnit built-in runner and run with the parameterized runner , a method that provides parameters in a parameterized run class is decorated with @parameters annotations, assigning values for each parameter in the construction method of the test class (the construction method is called by JUnit), and finally writing the Test class, which runs the test multiple times based on the number of groups of parameters.

8. In JUnit 4, if you want to run more than one Test at a time, you need to use two annotations:

@RunWith (Suite.class) and @suite.suiteclasses (), each of these annotations specifies the use of the Suite runner to run the test, and which test classes are run, where the @suiteclasses You can continue to specify the suite so that JUnit can then look for the test class inside, and always find the testing case that can be executed and execute it.

9. There are two ways to test a private method of a class:

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

2) Use reflection to invoke the private method of the target class in the test class (recommended).

10.TestSuite (test suite): Multiple tests can be combined to perform multiple tests at the same time.

Error and failure (failure) to be differentiated in 11.JUnit

1) error refers to the situation in the code that throws an exception, and so on, such as throwing out arrayindexoutofboundsexception, which is called error.

2) Failure means that we assert that the desired result is inconsistent with the actual execution of the program, or that the Fail () method is called directly, which is called failure.

Observer patterns used in 12.JUnit

Java Learning Chapter (v)---about JUnit 4

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.