您的位置:首页 > 编程语言 > Java开发

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

2017-11-03 18:11 791 查看
问题描述:

       当写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() {
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: