如何在运行集成测试之前启动spring-boot应用程序 [英] How to start spring-boot application before running integration test

查看:78
本文介绍了如何在运行集成测试之前启动spring-boot应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的spring-boot应用程序中使用Gatling插件来对作为应用程序一部分公开的REST API进行性能测试,因此需要在运行Gatling测试之前启动我的应用程序.

I am using Gatling plugin in my spring-boot application to do performance tests of the REST APIs exposed as part of the application hence need my application to be up before the gatling tests runs.

由于默认情况下,加特林执行与集成测试阶段相关联,因此我尝试分别对集成前阶段和集成后阶段使用启动-停止目标,但对于同一目标却低于错误:

Since Gatling execution is associated to integration-test phase by default so I tried using start-stop goals for pre-integration-phase and post-integration-phase respectively but getting below error for the same :

[[错误]无法执行目标org.springframework.boot:spring-boot-maven-plugin:1.5.1.RELEASE:start(集成前测试)项目:Spring应用程序未启动在配置的超时之前(30000ms-> [帮助1]]

[ [ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.1.RELEASE:start (pre-integration-test) on project : Spring application did not start before the configured timeout (30000ms -> [Help 1] ]

只需添加运行目标目标 mvn gatling:execute 在应用程序启动时就可以正常运行,但是我想将其作为maven阶段的一部分来运行.

Just to add that running gatling goal mvn gatling:execute runs just fine when the application is up but i want to run it as part of maven phases.

推荐答案

我将其与下面的代码一起使用.下面的代码将在所需的配置文件中启动spring应用程序,然后继续运行测试.ShutdownHook将关闭服务.

I got it working with the code I have below. The code below will start the spring application in the profile you want, and then proceed to run your tests. The ShutdownHook will turn the service off.

class MicroserviceServiceSimulation extends Simulation {

  System.setProperty("spring.profiles.default", System.getProperty("spring.profiles.default", "it"));

  val app: ConfigurableApplicationContext = SpringApplication.run(classOf[YourApplication])

  Runtime.getRuntime.addShutdownHook(new Thread() {
    override def run(): Unit = app.stop()
  })

}

这篇关于如何在运行集成测试之前启动spring-boot应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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