如何在没有SpringBootApplication的项目中启动

2021-06-01  本文已影响0人  十毛tenmao

有一些子项目其中包含了整套Web后台服务,但是本身并不作为独立的进程启动(没有SpringBootApplication),而是作为jar包被其他项目引用。这样单元测试启动的时候就会遇到错误java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test

解决方法

解决方法倒是很简单,既然是缺少SpringBootApplication,那么就在单元测试代码中,增加一个有SpringBootApplication的启动类就可以了。这样既不影响子项目的定位(不独立启动),又可以完成单元测试

@SpringBootApplication
class UtApplication {
    static void main(String[] args) {
        SpringApplication.run(UtApplication.class, args);
    }
}

参考

上一篇 下一篇

猜你喜欢

热点阅读