Chuanzhi podcast Java learning-Use of JUnit and log4j

Source: Internet
Author: User

I learned how to use JUnit for unit testing and log4j for log management. I took a simple note:

JUnit

JUnit is a Java testing framework for source code development. It is used to write and run reusable tests. This is an example of the unit test framework system xunit (for Java ). It includes the following features:
1. assertion)
2. test tools used to share shared Test Data
3. Test suites for convenient organization and operation testing
4. Graphic and text testing runner

 

JUnit 4 uses the annotation (annotation) in Java 5. The following describes several annotation commonly used in JUnit 4.
@ Before: Initialization Method
@ After: release resources
@ Test: Test method. The expected exception and timeout can be tested here.
@ Ignore: Test Method ignored
@ Beforeclass: All tests are executed only once and must be static void
@ Afterclass: only one test is executed and must be static void.
The execution sequence of a unit test case of JUnit 4 is:
@ Beforeclass-> @ before-> @ test-> @ After-> @ afterclass
The call sequence of each test method is as follows:
@ Before-> @ test-> @ after

 

 

Log4j

You can use log4j to manage logs,

1. log4j is an open-source project of Apache. It is a log operation package. By using log4j, you can specify the log output destination, such as the console, file, Cui component, and NT event recorder; you can also control the output format of each log. In addition, by defining the log information level, the log output can be controlled very carefully. The most interesting thing is that these functions can be flexibly configured through a configuration file, you do not need to modify the program code.

The output log in the application has three purposes:

L monitors variable changes in the Code and periodically records the data to files for statistical analysis by other applications.

L tracking the code runtime track as the basis for future audits

L act as a debugger in the integrated development environment, and print code debugging information to the file and console.

To output logs in a program, the most common practice is to embed statements in the Code. These print statements can output logs to the console or file, A good practice is to construct a log operation class to encapsulate such operations, rather than flooding a series of print statements with the main body of the Code.

 

2. with emphasis on reusable components, in addition to developing a reusable log operation class name from start to end, Apache provides us with a powerful ready-made log operation package log4j.

Log4j consists of three major components:

L logger: logger is responsible for generating logs and classifying and filtering log information. Generally speaking, it determines what log information should be output and what log information should be ignored.

L appender: defines the log information output destination, specifying where the log information should be output, such as the console, files, and network devices.

L layout: Specify the log output format

These three components work together to enable developers to record information based on log information categories and control the output format and location of log information during the running of the program.

A logger can have multiple appender, which means that the log information can be output to multiple devices at the same time. Each appender corresponds to a layout, which determines the format of the output log information.

 

Summary:

Log4j consists of three major components: logger, appender, and layout. Logger controls the output of log information; appender determines the output destination of log information; layout determines the output format of log information. Log4j allows you to flexibly configure these components in the configuration file. It is very convenient to use log4j in a program. You only need to obtain the logger first, read the configuration file, and configure the log4j environment. Then, you can output logs wherever necessary in the program, call the appropriate method of the logger class to generate logs.

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.