【问题标题】:springboottest how to prevent running applicationspringboottest如何防止运行应用程序
【发布时间】:2018-11-11 00:34:06
【问题描述】:

我有运行 Spring 批处理 ETL 的标准 Application 类:

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

我的 Junit 测试执行如下操作:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class MyServiceTest {

    @Autowired
    MyService myService;

    @Test
    public void testInsertions() {
        //do stuff with assertions
    }
}

我的问题是,当我执行 junit 测试时,应用程序也会启动 ETL,然后执行测试。如何阻止应用程序运行?

【问题讨论】:

    标签: spring-boot springjunit4classrunner


    【解决方案1】:

    我认为有很多选择,这实际上取决于您要达到的目标。

    其中一个选项是使用特定配置文件运行您的测试,例如 testing,并配置您的 ETL(我假设它们只是作业)属性或特定配置文件。

    例如:

    测试类

    @ActiveProfiles("testing")
    @RunWith(SpringJUnit4ClassRunner.class)
    @SpringBootTest
    public class MyServiceTest {
        ...
    }
    

    工作/ETL 课程

    @Component
    @Profile("!testing")
    public class JobEtlService {
    }
    

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-12
      • 2022-01-02
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 2019-03-01
      相关资源
      最近更新 更多