springboot单元测试两种方法实例详解
2020-03-11 17:51
841 查看
这篇文章主要介绍了springboot单元测试两种方法实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
springboot的单元测试,这里介绍两种方式,一种是在测试类中添加注解;另一种是在代码中启动项目的main方法中继承接口(也可以写在其他方法中)。
如 对查看数据库的连接池信息 进行单元测试
1. 在类上使用注解:
@RunWith(SpringRunner.class)
@SpringBootTest
@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 config 拦截器使用方法实例详解
- Spring boot进行参数校验的方法实例详解
- Java中分割字符串的两种方法实例详解
- SpringTest框架JUnit单元测试用例获取ApplicationContext实例的方法
- Spring加载properties文件的两种方式实例详解
- SpringTest框架JUnit单元测试用例获取ApplicationContext实例的方法
- spring boot application properties配置实例代码详解
- SpringBoot整合MyBatis逆向工程及 MyBatis通用Mapper实例详解
- 详解springboot之jackson的两种配置方式
- SpringTest框架JUnit单元测试用例获取ApplicationContext实例的方法
- Spring依赖注入的两种方式(根据实例详解)
- 详解Spring Boot中MyBatis的使用方法
- SpringBoot开发Junit单元测试方法
- Spring Boot (番外篇:使用idea创建springboot项目的两种方法)
- 详解springboot采用多数据源对JdbcTemplate配置的方法
- 详解SpringBoot实现JPA的save方法不更新null属性
- SpringTest框架JUnit单元测试用例获取ApplicationContext实例的方法
- SpringTest框架JUnit单元测试用例获取ApplicationContext实例的方法
- 详解Spring 两种注入的方式(Set和构造)实例
- 详解在SpringBoot应用中获取应用上下文方法