首页 > 其他分享 >21_Spring_日志框架和测试支持

21_Spring_日志框架和测试支持

时间:2023-03-04 13:31:52浏览次数:53  
标签:21 Spring springframework context org test import 日志 junit


 spring5框架自带了通用的日志封装,也可以整合自己的日志
 1)spring移除了 LOG4jConfigListener,官方建议使用log4j2
 2)spring5整合log4j2
导入log4j2依赖
 

<!--log4j2 依赖-->
<!--<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.14.0</version>
</dependency>-->
<!--slf4-impl 包含了log4j2 依赖-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.14.0</version>
<scope>test</scope>
</dependency>

21_Spring_日志框架和测试支持_Test

21_Spring_日志框架和测试支持_Test_02

 

在resources目录下准备log4j2.xml的配置文件
 

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="DEBUG">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>

21_Spring_日志框架和测试支持_xml_03

21_Spring_日志框架和测试支持_Test_04

 

spring5关于测试工具的支持

整合junit4
依赖的jar

 

<!--Junit4单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<!--spring test测试支持包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.5</version>
<scope>test</scope>
</dependency>

21_Spring_日志框架和测试支持_xml_05

21_Spring_日志框架和测试支持_Test_06

 

测试代码编写方式
 

package com.msb.test;
import com.msb.config.SpringConfig;
import com.msb.service.AccountService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.lang.Nullable;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @Author: Ma HaiYang
* @Description: MircoMessage:Mark_7001
*/
@RunWith(SpringJUnit4ClassRunner.class)// 指定测试支持类
@ContextConfiguration("classpath:applicationContext.xml")// 指定核心配置文件位置
public class Test2 {
@Autowired // 注入要获取的bean
private AccountService accountService;
@Test()
public void testTransaction(){
int rows = accountService.transMoney(1, 2, 100);
System.out.println(rows);
}
}

21_Spring_日志框架和测试支持_xml_07

21_Spring_日志框架和测试支持_Test_08

 

整合junit5

依赖的jar
 

<!--junit5单元测试-->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>

21_Spring_日志框架和测试支持_xml_09

21_Spring_日志框架和测试支持_spring_10

 

测试代码编写方式
 

package com.msb.test;
import com.msb.service.AccountService;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
/**
* @Author: Ma HaiYang
* @Description: MircoMessage:Mark_7001
*/
/*使用ExtentWith和ContextConfiguration注解*/
/*@ExtendWith(SpringExtension.class)
@ContextConfiguration("classpath:applicationContext.xml")*/
// 使用复合注解
@SpringJUnitConfig(locations = "classpath:applicationContext.xml")
public class Test3 {
@Autowired // 注入要获取的bean
private AccountService accountService;
@Test
public void testTransaction(){
int rows = accountService.transMoney(1, 2, 100);
System.out.println(rows);
}
}

21_Spring_日志框架和测试支持_Test_11

21_Spring_日志框架和测试支持_spring_12



标签:21,Spring,springframework,context,org,test,import,日志,junit
From: https://blog.51cto.com/u_15864767/6100026

相关文章

  • java——spring boot集成RabbitMQ——高级特效——死信代码示例
    首先,消息成为死信的条件:       首先看消息生产者,生产者和之前的一样,没什么变化(注意:后面统一把nomal改为normal了):          消费......
  • 21_Spring_日志框架和测试支持
    ​ spring5框架自带了通用的日志封装,也可以整合自己的日志 1)spring移除了LOG4jConfigListener,官方建议使用log4j2 2)spring5整合log4j2导入log4j2依赖 <......
  • Spring 事务的七种传播属性
    1.事务传播定义事务传播机制定义了多个包含了事务的方法在相互调用时,事务是如何在这些方法之间进行传递的。2.作用事务传播机制是解决一个事务在多个节点中传递的问......
  • Spring 事务梳理
    1. 事务介绍将一组操作封装成一个执行单元,要么全部成功,要么全部失败。当执行某个操作,例如转账操作时(分为先将钱从个人账户扣除和将他人账户新增两个操作),如果这两个操作不......
  • springcloud-openfeign调用远程服务
    openfeign是springcloud的子组件,使用restful方式请求服务,声明式风格。前提准备,一个调用方member,一个被调用方coupon,一个nacos注册中心,调用方和被调用方需先完成服务注册。......
  • Spring Boot @Scheduled 是同步还是异步,单线程还是多线程?
    @schedule刚开始用的时候回遇到一些坑,主要就是他的同步、异步、多线程的配置问题,这篇文章介绍了@schedule的使用方法,读者遇到问题时可以参考下。1.问题@schedule注解默......
  • 汇川AM401(Scanner) 和Easy521 (Adapter)的EIP通信
    1,导出Easy系列的EDS文件,配置Easy521的收发字节大小  2,导入Easy系列的EDS文件,配置AM401的收发字节大小    3,AM401作为Scanner的EIP通讯诊断代码......
  • F. Timofey and Black-White Tree 2100 (树 根号分治 思维)
    https://codeforces.com/problemset/problem/1790/F题意:给定一棵树,需要将其染为全黑,初始时只有一个点为黑色,给定一个序列c,按招顺序染色,要求每次染色后给出当前任意两黑点......
  • 线段树维护哈希 | CF213E Two Permutations + CF452F Permutation
    __终于学到了线段树维护xx,嘿嘿,嘿嘿嘿..树树:D做了两题,感觉知识点是维护区间相同不相同可以拿hash做,不连续的区间也可以拿hash维护主要是出得很有想法,太妙了要想得到hh1.......
  • springboot接入nacos注册中心
    1,引入依赖,所有模块都要用到,多模块里可以将依赖放到父模块或者公用模块<dependency><groupId>com.alibaba.cloud</groupId><artifactId......