首页 > 编程语言 > 详细

springboot的单元测试(总结两种)

时间:2019-11-09 17:22:28      阅读:188      评论:0      收藏:0      [点我收藏+]

1.如查看数据库的连接池信息

@RunWith(SpringRunner.class)
@SpringBootTest
public class RobotsApplicationTests {

    @Autowired
    DataSource dataSource;

    @Test
    public void test(){
        System.out.println(dataSource.getClass());
    }

}

2.或者继承CommandLineRunner接口

  CommandLineRunner:表示在项目启动后执行的功能,需要只需的内容写在其run()方法中,如:

@SpringBootApplication
@EnableScheduling
@ComponentScan(basePackages={"com.cmit.hall.plat","com.cmit.hall.pub"}) 
@ServletComponentScan(value= {"com.cmit.hall.pub.interceptor","com.cmit.hall.plat.config","com.cmit.hall.pub.session"})
@EnableRedisHttpSession(maxInactiveIntervalInSeconds=1800)
public class PlatApp implements CommandLineRunner {
    
    @Autowired
    DataSource dataSource;

    public static void main(String[] args) {
        SpringApplication.run(PlatApp.class, args);
    }
    
    @Override
    public void run(String... args) throws Exception {
        System.out.println(">>>>>>>>>>>>>>>服务启动执行,执行加载数据等操作<<<<<<<<<<<<<");
        System.out.println("DATASOURCE = " + dataSource);
    }
}

 

  

springboot的单元测试(总结两种)

原文:https://www.cnblogs.com/sun-flower1314/p/11799425.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!