Spring 启动应用程序未以 Maven 和 Karate 启动

Spring boot app not starting with Maven & Karate

我有这样的代码,当我通过 Intellij 运行 时,它会启动 spring 启动 ConsumeServiceApplication 应用程序,但是当我 运行 它通过 maven 然后它不会启动相同的 Spring 引导应用程序。

我是否遗漏了 pom.xml 中需要添加的内容?

import foo.ConsumeServiceApplication;
import com.intuit.karate.*;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.context.*;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = {
        ConsumeServiceApplication.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ContextConfiguration(classes = {AcceptanceTestConfiguration.class})
@ActiveProfiles("test")
public class AtddTest {

    private static final String CUCUMBER_OUTPUT_DIR = "target/cucumber";

    @Test
    public void testMyService() throws Exception {
        Results results = Runner.parallel(getClass(), 5, CUCUMBER_OUTPUT_DIR);
        Assertions.assertEquals(0, results.getFailCount());
    }

}

我意识到 SpringBootTest 中的 none 正在启动 Spring 启动应用程序,这是 Maven Surefire 插件的问题

详情请见

https://dzone.com/articles/why-your-junit-5-tests-are-not-running-under-maven

要为这个问题的答案添加更多信息:

  1. 在使用 mvn test 进行空手道测试之前,使用 spring-boot-maven-plugin 连接 spring 启动服务器,这足以满足此目的。 您可以使用示例 https://github.com/intuit/karate/blob/master/karate-demo/src/test/java/test/ServerStart.java,但我认为这比需要的更复杂。

  2. 对于 karate+spring-boot 的 Gatling 性能测试,您将需要 gatling-maven-plugin,结合 mvn verify 和 . scala 文件:

// Because Maven Failsafe will not start Spring-Boot before the Gatling test, we hard code it.
  val app: ConfigurableApplicationContext = SpringApplication.run(classOf[MyApplication])
  Runtime.getRuntime.addShutdownHook(new Thread() {
    override def run(): Unit = app.stop()
  })