黄瓜测试Spring Boot应用程序

时间:2015-06-05 13:24:46

标签: java spring spring-boot cucumber-jvm

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

这是我的设置

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

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

CucumberTest.java

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

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

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

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

感谢。

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

5 个答案:

答案 0 :(得分:13)

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

In short, the AbstractSpringTest class needs to have the following annotations: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = DemoApplication.class, loader = SpringApplicationContextLoader.class) @WebAppConfiguration @IntegrationTest

答案 1 :(得分:6)

我有类似的症状,我的黄瓜不会启动Spring背景......

原来我错过了(其中一个)以下依赖项:

<强>的build.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;

}

更新:SpringBoot 1.5.1

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

答案 2 :(得分:1)

继@jakehschwartz之后,如果您希望Web应用程序在随机可用端口上启动,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;
...}

答案 3 :(得分:0)

我做了类似的事情让Spring使用JUnit参数化测试。它应该是黄瓜的相同概念,但我还没有尝试过。我正在使用XML配置,因此可能会有所作为。

<强> 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 {
}

答案 4 :(得分:0)

首先,您需要确保在gradle中有applied spring-boot。调用gradle build将产生一个可运行的jar。而不是将您的清单调用Spring类作为主要内容,而是使用一个包装器在一个线程waits for it to settle down中启动它并运行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);
     }
}