Spring对日志框架和测试的支持

世上唯一不能复制的是时间,唯一不能重演的是人生,唯一不劳而获的是年龄。该怎么走,过什么样的生活,全凭自己的选择和努力。人生很贵,请别浪费!与智者为伍,与良善者同行。Spring对日志框架和测试的支持,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

spring5框架自带了通用的日志封装,也可以整合自己的日志

  • spring移除了 LOG4jConfigListener,官方建议使用log4j2
  • spring5整合log4j2

一、导入log4j2依赖

<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-slf4j-impl -->
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j-impl</artifactId>
    <version>2.17.1</version>
    <scope>test</scope>
</dependency>

二、在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 - %augus%n" />
        </Console>
    </Appenders>
    <Loggers>
        <Root level="debug">
            <AppenderRef ref="Console" />
        </Root>
    </Loggers>
</Configuration>

三、spring5关于测试工具的支持(整合junit4)

1.导入所需的依赖

<!-- junit4单元测试 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>

        <!-- spring-test 测试支持包 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.3.22</version>
            <scope>test</scope>
        </dependency>

2.测试代码编写方式

读取配置文件的方式就可以采用Junit中提供的注解来完成了,但是注意我这里是基于之前的类,需要将之前零xml实现的配置类SpringConfig禁用,不然会干扰

import com.augus.service.AccountService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


@RunWith(SpringJUnit4ClassRunner.class)// 指定测试支持类
@ContextConfiguration("classpath:applicationContext.xml")// 指定核心配置文件位置
public class Test1 {
    @Autowired
    private AccountService accountService;

    @Test
    public void testTransaction(){
        //调用方法
        int rs = accountService.transfer(1, 2, 500.0);
        System.out.println(rs);
    }

四、spring5关于测试工具的支持(整合junit5)

1.导入依赖的

<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>5.8.2</version>
    <scope>test</scope>
</dependency>

2.测试代码

import com.augus.service.AccountService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;


// 使用复合注解
@SpringJUnitConfig(locations = "classpath:applicationContext.xml")
public class Test1 {
    @Autowired
    private AccountService accountService;

    @Test
    public void testTransaction(){
        //调用方法
        int rs = accountService.transfer(1, 2, 500.0);
        System.out.println(rs);
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/253681.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!