[Spring]第七篇:日志框架和测试支持
admin
2024-03-03 06:07:14

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

一 日志框架 

导入log4j2依赖 

       
       
       
       
            org.apache.logging.log4j
            log4j-slf4j-impl
            2.14.0
            test
       

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

 

   
       
           
       

   

   
       
           
       

   

二 测试支持


spring5关于测试工具的支持

整合junit4
依赖的jar

       
       
            junit
            junit
            4.13.1
            test
       

       
       
            org.springframework
            spring-test
            5.3.5
            test
       

测试代码编写方式

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:
 * @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);
    }
}


整合junit5

依赖的jar

       
       
            org.junit.jupiter
            junit-jupiter-api
            5.7.0
            test
       

测试代码编写方式 

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);
    }
}

相关内容

热门资讯

联合国环境署呼吁大幅增加自然保... 转自:新华社新华社内罗毕1月22日电(记者由荟圆)联合国环境规划署22日在肯尼亚首都内罗毕发布《20...
苹果是控糖友好型水果 (来源:劳动午报)转自:劳动午报 看似普通的苹果,实则是一位低调的“全能型选手”。中医认为,苹果兼具...
头部家电企业剧透2026年发展... 转自:证券日报    本报记者 贾丽    在消费品以旧换新政策带动下,2025年家电类、通讯器材类...
最新或2023(历届)禁毒日宣... 禁毒日活动的开展可以提高中小学生的禁毒意识,形成全民抑制毒品、全民参与禁毒的社会氛围,为构建和谐XX...
最新或2023(历届)学校6.... 学校通过禁毒日活动的开展,提高大家的毒品安全意识,增强毒品防御能力。下面是太阳教育网为大家整理的活动...