springboot单元测试两种方法实例详解
这篇文章主要介绍了springboot单元测试两种方法实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
springboot的单元测试,这里介绍两种方式,一种是在测试类中添加注解;另一种是在代码中启动项目的main方法中继承接口(也可以写在其他方法中)。
如对查看数据库的连接池信息进行单元测试
1.在类上使用注解:
@RunWith(SpringRunner.class)
@SpringBootTest
@RunWith(SpringRunner.class)
@SpringBootTest
publicclassRobotsApplicationTests{
@Autowired
DataSourcedataSource;
@Test
publicvoidtest(){
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)
publicclassPlatAppimplementsCommandLineRunner{
@Autowired
DataSourcedataSource;
publicstaticvoidmain(String[]args){
SpringApplication.run(PlatApp.class,args);
}
@Override
publicvoidrun(String...args)throwsException{
System.out.println(">>>>>>>>>>>>>>>服务启动执行,执行加载数据等操作<<<<<<<<<<<<<");
System.out.println("DATASOURCE="+dataSource);
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下