1. Unit test
1.1 overview [understanding]
JUnit is a unit testing tool for Java programming language. JUnit is a very important testing tool
1.2 features [understanding]
- JUnit is an open source testing tool.
- Provide annotations to identify test methods.
- JUnit testing allows you to write code faster and improve quality.
- JUnit is elegant and concise. It's not that complicated and takes less time.
- JUnit displays progress in a bar. Green if it works well; If the operation fails, it turns red.
1.3 use steps [ application ]
-
Use steps
- Import JUnit's jar package into the project junit-4.9 jar
- Write a test method. The test method must be a public non static method with no parameters and no return value
- Use the @ Test annotation on the Test method, which is a Test method
- Select the test method and right-click to run the method through junit
-
Code example
public class JunitDemo1 { @Test public void add() { System.out.println(2 / 0); int a = 10; int b = 20; int sum = a + b; System.out.println(sum); } }
1.4 relevant notes [ application ]
-
Annotation description
annotation meaning @Test Means to test the method @Before Run before the method being tested @After Run after method tested -
Code example
public class JunitDemo2 { @Before public void before() { // Execute before executing the test code, which is generally used for initialization operation System.out.println("before"); } @Test public void test() { // Test code to execute System.out.println("test"); } @After public void after() { // It is executed after the test code is executed, which is generally used to release resources System.out.println("after"); } }
3. Log
3.1 overview [understanding]
-
summary
The log in the program can be used to record bit by bit when the program is running. And can be stored permanently.
-
Difference between log and output statement
Output statement Log technology Cancel log The code needs to be modified, and the flexibility is poor There is no need to modify the code, which is more flexible output location Console only Log information can be written to a file or database Multithreading And business code are in the same thread Multi thread logging does not affect the performance of business code
3.2 log architecture and Log4J [understanding]
-
Architecture
-
Log4J
Log4j is an open source project of Apache.
By using Log4j, we can control the destination of log information delivery to console, file, etc.
We can also control the output format of each log.
By defining the level of each log information, we can control the log generation process in more detail.
The most interesting thing is that these can be flexibly configured through a configuration file without modifying the application code.
-
Apache Foundation
The Apache Software Foundation (ASF) is a non-profit organization established to support open source software projects.
3.3 introduction case [application]
-
Use steps
- Import the related jar package of log4j
- Write log4j configuration file
- The object that gets the log in code
- Log information according to level settings
-
Code example
// The configuration file of log4j is named log4j Properties in the src root directory log4j.rootLogger=debug,my,fileAppender ### direct log messages to my ### log4j.appender.my=org.apache.log4j.ConsoleAppender log4j.appender.my.ImmediateFlush = true log4j.appender.my.Target=System.out log4j.appender.my.layout=org.apache.log4j.PatternLayout log4j.appender.my.layout.ConversionPattern=%d %t %5p %c{1}:%L - %m%n # fileAppender��ʾ log4j.appender.fileAppender=org.apache.log4j.FileAppender log4j.appender.fileAppender.ImmediateFlush = true log4j.appender.fileAppender.Append=true log4j.appender.fileAppender.File=D:/log4j-log.log log4j.appender.fileAppender.layout=org.apache.log4j.PatternLayout log4j.appender.fileAppender.layout.ConversionPattern=%d %5p %c{1}:%L - %m%n // Test class public class Log4JTest01 { //Use the api of log4j to get the log object //Disadvantages: if we change the log implementation class in the future, the following code needs to be changed //Not recommended //private static final Logger LOGGER = Logger.getLogger(Log4JTest01.class); //Use the api in slf4j to get the log object //Benefit: if we change the implementation class of log in the future, the following code does not need to be modified //Recommended use private static final Logger LOGGER = LoggerFactory.getLogger(Log4JTest01.class); public static void main(String[] args) { //1. Import jar package //2. Prepare configuration file //3. Get the log object in the code //4. Set the log information according to the log level LOGGER.debug("debug Level log"); LOGGER.info("info Level log"); LOGGER.warn("warn Level log"); LOGGER.error("error Level log"); } }
3.4 detailed description of configuration file [ understanding ]
-
Three cores
-
Loggers log level
There are five common levels of Loggers components in this system: DEBUG, INFO, WARN, ERROR and FATAL.
DEBUG < INFO < WARN < ERROR < FATAL.
Log4j has a rule: only log information with a level not lower than the set level is output.
-
Where the Appenders log is to be output
Output logs to different places, such as Console, Files, etc.
- org.apache.log4j.ConsoleAppender
- org.apache.log4j.FileAppender (file)
-
Layouts log output format
You can specify the format of log output according to your preferences
Common layout managers:
org.apache.log4j.PatternLayout (layout mode can be specified flexibly)
org.apache.log4j.SimpleLayout (level and information string containing log information)
org.apache.log4j.TTCCLayout((including log generation time, thread, category and other information)
-
-
Configure root Logger
-
format
log4j.rootLogger = log level, appenderName1, appenderName2
-
log level
OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL or custom level.
-
appenderName1
Specifies where the log information is to be output. Multiple output destinations can be specified at the same time, separated by commas.
For example: log4j rootLogger=INFO,ca,fa
-
-
Common options for ConsoleAppender
-
ImmediateFlush=true
Indicates that all messages will be output immediately. If it is set to false, it will not be output. The default value is true.
-
Target=System.err
The default value is system out.
-
-
FileAppender common options
-
ImmediateFlush=true
Indicates that all messages will be output immediately. If it is set to false, it will not be output. The default value is true
-
Append=false
true indicates that the message is added to the specified file, and the original message is not overwritten.
false overwrites the message with the specified file content. The default value is true.
-
File=D:/logs/logging.log4j
Specifies that the message is output to logging Log4j file
-
-
Common options for PatternLayout
-
ConversionPattern=%m%n
Sets the format in which messages are displayed
-
3.5 application in the project [ application ]
-
step
- Import related dependencies
- Copy the properties configuration file in the data to the src directory
- The object that gets the log in code
- Log information according to level settings
-
code implementation
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.Scanner; public class MyLog4j { //Get log object private static final Logger LOGGER = LoggerFactory.getLogger(MyLog4j.class); public static void main(String[] args) { Scanner sc = new Scanner(System.in); //Enter a number on the keyboard String number = sc.nextLine(); //Type conversion try { int result = Integer.parseInt(number); //System.out.println("type conversion succeeded" + result); LOGGER.info("Type conversion succeeded" + result); } catch (NumberFormatException e) { //System.out.println("type conversion failed, please enter an integer"); LOGGER.info("Type conversion failed, please enter an integer"); } } }