【问题标题】:Stop Spring Boot Test from hitting @PostContruct of SpringBootApplication class阻止 Spring Boot 测试点击 SpringBootApplication 类的 @PostContruct
【发布时间】:2017-04-02 15:46:44
【问题描述】:

我有一个 SpringBootApplication 类,它有一个类似 @PostConstruct 的方法(它初始化数据库连接的类型):

@SpringBootApplication
public class SpringBootApp extends WebMvcConfigurerAdapter {

    public static boolean workOffline = false;
    private boolean setupSchema = false;
    private IGraphService graphService;
    private DbC conf;

    @Autowired
    public SpringBootApp(IGraphService graphService, DbC conf)
    {
        this.graphService = graphService;
        this.conf = conf;
    }

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

    @PostConstruct
    public void initializeDB() {
        if (workOffline) {
            conf.setupOfflineEnvironment();
            return;
        }
        else {
            conf.setupProdEnvironment();
        }
        if (setupSchema) {
            graphService.setupTestUsers();
        }
    }
}

我也在使用 extend 这个基类的 Spring Boot 测试:

@RunWith(SpringRunner.class)
@Ignore
@SpringBootTest
public class BaseTest {

    @Before
    public void beforeTest() {

        if (SpringBootApp.workOffline) {
            conf.setupOfflineEnvironment();
        } else {
            conf.setupTestEnvironment();
        }

        graphService.setupTestUsers();}
    @After
    public void afterTest() {
        graphService.deleteAllData();
    }
}

我的测试在tests/ 下,而我的源代码在src/

不幸的是,有些情况下beforeTest() 会在@PostConstuct 之前执行,有些情况下它会在之后执行.. 有没有办法让我的测试使用@SprinbBootTest 运行而无需输入/构造SpringBootApp 类有吗?

谢谢!

【问题讨论】:

  • 您是否尝试过使用 Spring 配置文件和配置注释?例如,您可以使用@Profile("application") 注释SpringBootApp 类。然后将您的@Before@After 注释和方法抽象到一个TestConfig 类中,该类用@Configuration@Profile("test"). 注释,最后在您的BaseTest 类(或其他测试类)中,您可以用@ 注释它987654342@。这应该会阻止您的 SpringBootApp 类被初始化。这样的事情有用吗?
  • 我可能会通过 spring 属性管理 workOffline,然后使用 SpringBootTest#properties 将属性设置为 false
  • @ninj 很有趣!您能否举例说明如何使用 SprinbBoot 将变量设置为特定值?谢谢
  • 我的问题本质上是想以某种方式对@SpringBootApplication 说我现在正在实际运行测试,这不是实际的应用程序.. 有没有办法做到这一点?
  • 这只能通过定义配置类来工作。 Spring 的 blogdocs 声明 @SpringBootTest :“测试将首先尝试从任何内部类加载 @Configuration,如果失败,它将搜索您的主要 @SpringBootApplication 类。” "搜索算法从包含测试的包开始,直到找到@SpringBootApplication 或@SpringBootConfiguration 注释类"

标签: java spring spring-boot spring-boot-test


【解决方案1】:

根据要求,这里尝试使用 spring 属性(没有方便的 IDE,所以请原谅任何错误)

您可以使用SpringBootTest#properties 为您的测试设置属性

@RunWith(SpringRunner.class)
@Ignore
@SpringBootTest(properties="springBootApp.workOffline=true")
public class BaseTest {
    @Before
    public void beforeTest() { /* setup */ }

    @After
    public void afterTest() { /* teardown */ }
}

既然我们知道我们可以在测试中设置spring属性,我们就可以设置应用程序使用该属性了:

@SpringBootApplication
public class SpringBootApp extends WebMvcConfigurerAdapter {

    @Value("${springBootApp.workOffline:false}")
    private boolean workOffline = false;

    @Value("${springBootApp.setupSchema:false}")
    private boolean setupSchema = false;

    @PostConstruct
    public void initializeDB() {
        if (workOffline) {
            // ...
        } else {
            // ...
        }
        if (setupSchema) {
            // ...
        }
    }
}

在我写下这个答案时,我注意到了几件事:

无论如何,祝你好运!

【讨论】:

    猜你喜欢
    • 2016-01-22
    • 2019-03-17
    • 2018-06-10
    • 1970-01-01
    • 2021-06-20
    • 2018-11-19
    • 2017-06-03
    • 1970-01-01
    • 2019-01-21
    相关资源
    最近更新 更多