springboot~没有启动类项目的单元测试
原创
©著作权归作者所有:来自51CTO博客作者仓储大叔的原创作品,请联系作者获取转载授权,否则将追究法律责任
一般对于公用包来说,是不需要springboot启动类的,它只是一个工具,而对于包的健壮性来说,单元测试是必须的,而如果你使用下面代码声明类,将会出现,“无法找到启动类”
@RunWith(SpringRunner.class)
@SpringBootTest()
public class RedisLockTest {}
对于@SpringBootTest这个注解来说,它帮助我们将main程序中的包都注册到ioc容器里,方便我们在单元测试项目中,直接注入这些对象,而对于没有启动类的工具包来说,这种方便不适用。
- TestPropertySource 手动注册配置文件
- ContextConfiguration 手动注册bean
@RunWith(SpringRunner.class)//spring运行器
@TestPropertySource("classpath:application.properties") //配置文件注入
@ContextConfiguration(classes=AsposeWordToHtml.class)
public class PoiWordToHtmlTest {}
这样,在@Test单元测试里,就可以通过@Value注入配置文件 ,使用@Autowired注入bean对象了
作者:仓储大叔,张占岭,
荣誉:微软MVP