SpringBoot 版本2.1.1
通过https://start.spring.io 导出的工程骨架,默认有单元测试类直接添加测试方法即可,自所以有这个笔记是因为,1.本笨蛋今天第一次用SpringBoot,而且想在以后使用便利的Junit测试工具进行日常代码的自测,不想一次次通过打包进行测试(蠢啊我);2.看网上的视频教程自己做的时候发现不是看到的那么回事,估计大半是SpringBoot版本的问题。
所用的全局配置文件:
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/spring spring.datasource.username=root spring.datasource.password=root #开启正向工程(自动创建没有的数据库表) spring.jpa.hibernate.ddl-auto=update
SpringBoot入口文件的注解配置:
@ComponentScan(basePackages = "com.lhn") @EnableJpaRepositories(basePackages = "com.lhn") @EntityScan(basePackages ="com.lhn") public class App{ public static void main(String[] args) { SpringApplication.run(App.class, args); } }
测试用例:
package com.lhn.u; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) //也可以用SpringJUnit4ClassRunner.class @SpringBootTest //不用指出应用的入口文件 public class UApplicationTests { @Test public void contextLoads() { } }