简体   繁体   English

春季启动测试启动应用程序

[英]Spring boot test starts Application

I have written simple Spring boot command line application. 我已经编写了简单的Spring boot命令行应用程序。 I am implementing the unit test but I have figured out that the application is run as well. 我正在执行单元测试,但我发现该应用程序也正在运行。 How to stop it? 如何停止呢?

Main app 主应用

@SpringBootApplication
public class Application {
  @Bean
  public CommandLineRunner demo(TransactionLogRepository repository) {
    return (args) -> {
        log.info("Application starts");

Test 测试

@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTests {

  @Autowired
  private ComputeBrand normalizer;

  @Test
  public void test() {
    log.info("Test starts");

Logs 日志

16:27:12.791 [main] DEBUG org.springframework.test.context.junit4.SpringJUnit4ClassRunner - SpringJUnit4ClassRunner constructor called with [class ApplicationTests]
2018-06-21 16:27:14.874  INFO 40980 --- [           main] .v.c.ApplicationTests : Starting ApplicationTests on DSCX516258 with PID 40980 (started by leos.literak
2018-06-21 16:27:14.877  INFO 40980 --- [           main] .v.c.ApplicationTests : No active profile set, falling back to default profiles: default
2018-06-21 16:27:14.939  INFO 40980 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@13f17eb4: startup date [Thu Jun 21 16:27:14 CEST 2018]; root of context hierarchy
2018-06-21 16:27:22.485  INFO 40980 --- [           main] .v.c.ApplicationTests : Started ApplicationTests in 8.45 seconds (JVM running for 11.151)
2018-06-21 16:27:22.513  INFO 40980 --- [           main] c.a.p.v..Application  : Application starts
2018-06-21 16:27:23.373  INFO 40980 --- [           main] c.a.p.v.c.ApplicationTests : Test starts

Where is the problem? 问题出在哪儿?

I have created following workaround: 我创建了以下解决方法:

application.properties application.properties

 unitTest=false

Application 应用

@SpringBootApplication
public class Application {
    @Value("${unitTest}")
    boolean unitTest = false;

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public CommandLineRunner processTransactions(TransactionLogRepository repository) {
        return (args) -> {
            if (unitTest) {
                return;
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM