【问题标题】:SpringBootTest support for running diffent SpringBoot Application InstancesSpringBootTest 支持运行不同的 SpringBoot 应用程序实例
【发布时间】:2017-05-04 16:59:10
【问题描述】:

使用 SpringBoot,我可以轻松地将相同的应用程序作为具有不同配置的不同实例运行,例如:

@SpringBootApplication
@ComponentScan({"..."})
public class Application {

    public static void main(String[] args) {
        start(Application.class)
            .properties("notification.sender.app.name=SomeApp", "notification.this.app.name =AnotherApp", "server.port=${first.port:9010}").run(args);
        start(Application.class)
            .properties("first.app.name=AnotherApp", "second.app.name =SomeApp", "server.port=${second.port:9020}").run(args);
    }

    private static SpringApplicationBuilder start(Class<?>... sources) {
        return new SpringApplicationBuilder(sources).bannerMode(Mode.OFF);
    }
}

这太棒了,特别是用于测试应用程序间通信的东西。

我现在正在尝试使用 @SpringBootTest 实现相同的目标,以针对正在运行的应用程序实例运行单元测试。

可能很容易,但我没有。

【问题讨论】:

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


    【解决方案1】:

    我在 Junit 测试中使用 @ContextConfigurationSpringRunner.class 作为测试运行者的方式:

    @Before
    public void startUp() {
        someAppContext = new SpringApplicationBuilder(Application.class)
                .properties("notification.app.name=SomeApp", "server.port=" + someAppPort).run();
        anotherAppContext = new SpringApplicationBuilder(Application.class)
                .properties("notification.app.name=AnotherApp", "server.port=" + anotherAppPort).run();
    }
    

    其中someAppPortanotherAppPort 配置为@Value

    【讨论】:

    • 你能解释一下这个解决方案吗?
    • @afe 您需要一个 Spring Boot Application.class ,并根据需要进行注释。在使用 @RunWith(SpringRunner.class) 运行的 Test Class 中,有一个 setup 方法 startup() ,它在两个不同的端口上运行相同的 Spring Boot Application.class 两次。
    • 感谢您提供详细信息。我在 java 中一直需要的是你导入的包的名称和 gradle/maven 存储库。
    猜你喜欢
    • 2023-03-16
    • 2018-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-21
    • 2020-01-09
    • 2014-11-07
    相关资源
    最近更新 更多