Java Cucumber 测试 Spring Boot 应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30667729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 09:57:28  来源:igfitidea点击:

Cucumber Test a Spring Boot Application

javaspringspring-bootcucumber-jvm

提问by jakehschwartz

Does anyone know where I can find a sample application where Cucumber is used to test a Spring Boot application through Gradle? I can run the tests fine starting the server on the cmd line and using my IDE, but I need to be able to run them all programmatically on the CI server. I saw the answer on here but that solution did not work for me, most likely because I have multiple step def files.

有谁知道在哪里可以找到使用 Cucumber 通过 Gradle 测试 Spring Boot 应用程序的示例应用程序?我可以在 cmd 行上启动服务器并使用我的 IDE 运行测试,但我需要能够在 CI 服务器上以编程方式运行它们。我在这里看到了答案,但该解决方案对我不起作用,很可能是因为我有多个步骤 def 文件。

Here is my setup

这是我的设置

build.grade (Mentioned in the other question)

build.grade(在另一个问题中提到)

testCompile ("org.springframework.boot:spring-boot-starter-test",
    ...
    "info.cukes:cucumber-spring:${cucumberVersion}")

CucumberTest.java

黄瓜测试.java

@RunWith(Cucumber.class)
@CucumberOptions(format = "pretty", features = "src/test/resources")
public class CucumberTest{
}

AbstractSpringTest.java (Extended by all the StepDef files)

AbstractSpringTest.java(由所有 StepDef 文件扩展)

@SpringApplicationConfiguration(classes = Application.class)
@RunWith(SpringJUnit4ClassRunner.class)
@Ignore
public abstract class AbstractSpringTest
{ ... }

It's not doing the correct thing on start up because its 1. trying to initialize my step def files and 2. My application is not started and the cucumber tests cannot make a connection.

它在启动时没有做正确的事情,因为它 1. 尝试初始化我的 step def 文件和 2. 我的应用程序未启动并且黄瓜测试无法建立连接。

Thanks.

谢谢。

EDIT: Or if someone can tell me how to start and stop the application using gradle, that would be acceptable as well.

编辑:或者如果有人可以告诉我如何使用 gradle 启动和停止应用程序,那也是可以接受的。

采纳答案by jakehschwartz

I have solved the issue with some help from this question.

我已经在这个问题的帮助下解决了这个问题

Here is the repository with the answer: https://github.com/jakehschwartz/spring-boot-cucumber-example

这是带有答案的存储库:https: //github.com/jakehschwartz/spring-boot-cucumber-example

In short, the AbstractSpringTestclass needs to have the following annotations:

总之,AbstractSpringTest类需要有以下注解:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = DemoApplication.class, loader = SpringApplicationContextLoader.class)
@WebAppConfiguration
@IntegrationTest

回答by LucasP

I did something like this to get Spring to work with JUnit parameterized tests. It should be the same concept for Cucumber, but I haven't tried it. I was using XML configuration, so that might make a difference.

我做了这样的事情来让 Spring 与 JUnit 参数化测试一起工作。黄瓜应该是相同的概念,但我还没有尝试过。我使用的是 XML 配置,所以这可能会有所作为。

RunWithSpringJUnit4

RunWithSpringJUnit4

public abstract class RunWithSpringJUnit4 {

    private TestContextManager testContextManager;

    public RunWithSpringJUnit4() {
        try {
            this.testContextManager = new TestContextManager(getClass());
            this.testContextManager.prepareTestInstance(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

CucumberTest

黄瓜测试

@RunWith(Cucumber.class)
@CucumberOptions(format = "pretty", features = "src/test/resources")
public class CucumberTest extends RunWithSpringJUnit4 {
}

回答by hd1

First, you'll need to ensure that you have applied spring-bootin gradle. Invoke gradle buildwhich will produce a runnable jar. Instead of having your manifest call for the Spring class as your main, have a wrapper that starts it in a thread, waits for it to settle downand runs Cucumber:

首先,您需要确保已在 gradle 中应用了 spring-boot。调用gradle build将生成一个可运行的 jar。不要将 Spring 类的清单调用作为主要调用,而是使用一个包装器在线程中启动它,等待它稳定下来并运行Cucumber

@RunWith(Cucumber.class)
public class LucasePsCucumberTest implements Runnable {
    public static void main(String[] args) {
        Thread t = new Thread(this);
        t.start();
        // wait for t
        cucumber.api.cli.Main(null);
     }
}

回答by Joe Stepowski

Further to @jakehschwartz, if you want the web app to start on a random available port, AbstractSpringTest needs:

进一步@jakehschwartz,如果您希望网络应用程序在随机可用端口上启动,AbstractSpringTest 需要:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Application.class, loader = SpringApplicationContextLoader.class)
@WebIntegrationTest({"server.port=0"})
public abstract class AbstractSpringTest {

        @Value("${local.server.port}")
        protected int serverPort;
...}

回答by Nick Grealy

I had a similar symptom, my cucumber wouldn't start up the Spring context...

我有类似的症状,我的黄瓜不会启动 Spring 上下文......

Turns out I had missed (one of) the following dependencies:

结果我错过了以下依赖项(之一):

build.gradle

构建.gradle

testCompile "info.cukes:cucumber-junit:1.2.4"
testCompile "info.cukes:cucumber-java:1.2.4"
testCompile "info.cukes:cucumber-spring:1.2.4"

StepDefs.java

步骤定义文件

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(
        loader = SpringApplicationContextLoader.class,
        classes = Application.class
)
@WebIntegrationTest(randomPort = true)
public class StepDefs {

    @Value("${local.server.port}")
    int port;

}

Update: SpringBoot 1.5.1

更新:SpringBoot 1.5.1

@ContextConfiguration(
        loader = SpringBootContextLoader.class,
        classes = Application.class
)