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

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);
    }
}

  

猜你喜欢

转载自www.cnblogs.com/sun-flower1314/p/11799425.html