如何集成测试自定义Spring Boot样式入门库的自动配置? [英] How to integration test auto configuration for a custom Spring Boot style starter library?

查看:99
本文介绍了如何集成测试自定义Spring Boot样式入门库的自动配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个提供一些功能的库,这些功能在我使用的多个不同的Spring Boot应用程序之间共享.

I am writing a library to provide some functionality that is shared between multiple different Spring Boot applications that I work with.

我想做一些类似于许多Spring Boot启动程序库提供的自动配置的事情.通过这种方法或其他一些简单的声明性方法,可以将我的库与使用它的应用程序的ApplicationContext集成.

I would like to do something similar to the auto-configuration that is provided by the many Spring Boot starter libraries exist. That, or some other simple declarative way to integrate my library with the ApplicationContext of the apps using it.

我找到了一些资源来解释自动配置的工作原理.我可以解决以上问题.

I have found some resources explaining how auto configuration works. I can figure out the above problem.

但是,我还没有找到任何可以作为我的库测试套件一部分进行测试的良好示例,该套件可以与Spring Boot应用程序适当地集成.理想情况下,我将直接为库测试而启动一个在库的测试中编写的简单Spring Boot应用程序,为它添加正确的注释,然后断言可以配置正确的bean.

However, I have not been able to find any good examples of how I can test as part of my library's test suite that it suitably integrates with a Spring Boot application. Ideally, I would start up a simple Spring Boot app written in the library's test directly just for the sake of testing, add the right annotation to it, and be able to assert that the correct beans are then configured.

我尝试创建一个可以执行此操作的TestApplication类,并使用SpringBootTest批注编写集成测试,但是TestApplication在测试开始前从未启动.

I have tried creating a TestApplication class that does that and writing integration tests using the SpringBootTest annotation but the TestApplication was never started before my test started.

如何仅出于测试我的库的目的启动一个简单的应用程序?我的测试是用Spock和Spock-Spring编写的,以防与其他测试框架发生变化.

What can I do to start up a simple app like that solely for the purpose of testing my library? My tests are written with Spock and Spock-Spring in case that changes things versus other test frameworks.

推荐答案

我能够使其与以下测试类一起使用:

I was able to make it work with the following test class:

@SpringBootTest
@ContextConfiguration(classes = TestApplication)
class DummyIntegrationSpec extends Specification {

    @Autowired
    DummyService dummyService

    void 'dummy service should exist'() {
        expect:
        dummyService.getMessage() == DummyConfiguration.MESSAGE
    }
}

和此TestApplication类位于src/test/groovy/com/example/project/TestApplication.groovy

and this TestApplication class at src/test/groovy/com/example/project/TestApplication.groovy

@SpringBootApplication(scanBasePackages = 'com.example.project.config')
@EnableAutoConfiguration
class TestApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(TestApplication)
    }

    static void main(String[] args) {
        SpringApplication.run(TestApplication, args)
    }
}

当我将TestApplication类从src/main移到src/test时,为了使TestApplication启动并加载正确的上下文,我必须进行两个关键更改:

The two key changes I had to make in order for the TestApplication to start and load the correct context when I moved my TestApplication class from src/main to src/test were:

  • 需要将TestApplication类添加到ContextConfiguration批注中

  • the TestApplication class needed to be added to the ContextConfiguration annotation

我的库的Java配置文件所在的包需要添加到SpringBootApplication scanBasePackages字段中

the package that my library's Java config files live in needed to be added to the SpringBootApplication scanBasePackages field

库的自动配置的确与提供的链接tom中提到的结构类似.

The library auto-configuration does follow a similar structure to the one mentioned in the link tom provided.

这篇关于如何集成测试自定义Spring Boot样式入门库的自动配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆