Spring-boot中添加commandLineRunner之后,写单元测试会自动执行commandLineRunner的解决方案

来源:互联网 发布:苹果6s蜂窝数据打不开 编辑:程序博客网 时间:2024/05/29 20:02

问题描述:

       当写spring-boot的控制台程序,或者为web程序增加了CommandLineRunner之后,在写单元测试时,会自动执行CommandLineRunner中的代码,导致单元测试无法正常进行,严重影响开发进度和效率。

解决方案:

        谷歌到一个日文的解决方案:点击打开链接

        方法可以看代码部分,这里照搬一下:

         

        

// “!test” 表示该CommandLineRunner中的程序会在除了名为test的profile之外的地方执行,即排除掉test@Profile("!test")public class App implements CommandLineRunner {    public static void main(String[] args) {        SpringApplication.run(App.class, args);    }    @Override    public void run(String... arg0) throws Exception {        System.out.println("run!");    }}@RunWith(SpringRunner.class)@SpringBootTest// 这里讲该测试文件标记为test@ActiveProfiles("test")public class AppTest {    @Test    public void contextLoads() {    }}

阅读全文
0 0