21_Spring_日志框架和测试支持

21_Spring_日志框架和测试支持

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

1)spring移除了 LOG4jConfigListener,官方建议使用log4j2

2)spring5整合log4j2

导入log4j2依赖

  1. <!--
  2. org.apache.logging.log4j
  3. log4j-core
  4. 2.14.0
  5. -->
  6. org.apache.logging.log4j
  7. log4j-slf4j-impl
  8. 2.14.0
  9. test

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

spring5关于测试工具的支持

整合junit4

依赖的jar

  1. junit
  2. junit
  3. 4.13.1
  4. test
  5. org.springframework
  6. spring-test
  7. 5.3.5
  8. test

测试代码编写方式

  1. package com.msb.test;
  2. import com.msb.config.SpringConfig;
  3. import com.msb.service.AccountService;
  4. import org.junit.Test;
  5. import org.junit.runner.RunWith;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.context.ApplicationContext;
  8. import
    org.springframework.context.annotation.AnnotationConfigApplicationContext;
  9. import org.springframework.context.support.ClassPathXmlApplicationContext;
  10. import org.springframework.lang.Nullable;
  11. import org.springframework.test.context.ContextConfiguration;
  12. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  13. /**
    • @Author: Ma HaiYang
    • @Description: MircoMessage:Mark_7001
  14. */
  15. @RunWith(SpringJUnit4ClassRunner.class)// 指定测试支持类
  16. @ContextConfiguration("classpath:applicationContext.xml")// 指定核心配置文件位置
  17. public class Test2 {
  18. @Autowired // 注入要获取的bean
  19. private AccountService accountService;
  20. @Test()
  21. public void testTransaction(){
  22. int rows = accountService.transMoney(1, 2, 100);
  23. System.out.println(rows);
  24. }
  25. }

整合junit5

依赖的jar

  1. org.junit.jupiter
  2. junit-jupiter-api
  3. 5.7.0
  4. test

测试代码编写方式

  1. package com.msb.test;
  2. import com.msb.service.AccountService;
  3. import org.junit.jupiter.api.Test;
  4. import org.junit.jupiter.api.extension.ExtendWith;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.test.context.ContextConfiguration;
  7. import org.springframework.test.context.junit.jupiter.SpringExtension;
  8. import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
  9. /**
    • @Author: Ma HaiYang
    • @Description: MircoMessage:Mark_7001
  10. */
  11. /使用ExtentWith和ContextConfiguration注解/
  12. /*@ExtendWith(SpringExtension.class)
  13. @ContextConfiguration("classpath:applicationContext.xml")*/
  14. // 使用复合注解
  15. @SpringJUnitConfig(locations = "classpath:applicationContext.xml")
  16. public class Test3 {
  17. @Autowired // 注入要获取的bean
  18. private AccountService accountService;
  19. @Test
  20. public void testTransaction(){
  21. int rows = accountService.transMoney(1, 2, 100);
  22. System.out.println(rows);
  23. }
  24. }

posted @ 2023-07-30 12:42  AidenDong  阅读(44)  评论(0)    收藏  举报