简体   繁体   English

Cucumber 测试 Spring Boot 应用程序

[英]Cucumber Test a Spring Boot Application

Does anyone know where I can find a sample application where Cucumber is used to test a Spring Boot application through Gradle?有谁知道在哪里可以找到使用 Cucumber 通过 Gradle 测试 Spring Boot 应用程序的示例应用程序? 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.我可以在 cmd 行上启动服务器并使用我的 IDE 运行测试,但我需要能够在 CI 服务器上以编程方式运行它们。 I saw the answer on here but that solution did not work for me, most likely because I have multiple step def files.我在这里看到了答案,但该解决方案对我不起作用,很可能是因为我有多个步骤 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 启动和停止应用程序,那也是可以接受的。

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 AbstractSpringTest class needs to have the following annotations:总之, AbstractSpringTest类需要有以下注解:

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

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
)

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;
...}

I did something like this to get Spring to work with JUnit parameterized tests.我做了这样的事情来让 Spring 与 JUnit 参数化测试一起工作。 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.我使用的是 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 {
}

First, you'll need to ensure that you have applied spring-boot in gradle.首先,您需要确保已在 gradle 中应用了 spring-boot Invoke gradle build which will produce a runnable jar.调用gradle build将生成一个可运行的 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 down and runs Cucumber :不要将 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);
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Junit 5 的 Spring Boot Cucumber 测试不会加载 application-test.properties - Spring Boot Cucumber Test with Junit 5 will not load application-test.properties 在 Cucumber 测试模块中使用主应用程序数据源 - Spring-boot 应用程序 - Use main application datasource in cucumber test module - Spring-boot application Spring Boot的黄瓜测试可以在“mvn test”中运行,但不能在“mvn verify”中运行 - Cucumber test for Spring Boot can run in “mvn test” but not in “mvn verify” 在 Spring Boot 应用程序中测试 websocket - Test websocket in spring boot application 春季启动测试启动应用程序 - Spring boot test starts Application 使用Arquillian测试Spring Boot应用程序 - Test Spring boot application with Arquillian 尝试为 Spring Boot App 的 Cucumber 集成测试加载不同的属性? - Trying to load different properties for cucumber integration test of Spring Boot App? Cucumber 测试用例未通过 Spring 启动 Jar 运行 - Cucumber test case not running via Spring Boot Jar Spring引导无法从黄瓜测试上下文创建数据源bean - Spring boot cannot create datasource bean from cucumber test context 从 Spring 引导 Cucumber 测试中的属性文件动态更改值 - Dynamically change value from a property file in Spring Boot Cucumber test
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM