26

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.

Here is my setup

build.grade (Mentioned in the other question)

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 (Extended by all the StepDef files)

@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.

Thanks.

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

7
  • in your gradle configuration start spring-boot before you execute your cucumber test cases. Because your spring-boot app should be in a running condition before executing cucumber test cases. Commented Jun 5, 2015 at 14:10
  • I don't know much groovy/gradle so could you give me an example? It starts itself for my integration tests so I figured I could use a similar mechanism. Commented Jun 5, 2015 at 15:23
  • have you tried @IntegrationTest . this will fire up your webserver etc as far as i know. i also use ` testCompile group: 'info.cukes', name: 'cucumber-junit', version:cucumberVersion` next to cucumber-spring. can you run your cucumber tests as junit tests in your ide? Commented Jun 9, 2015 at 12:33
  • Yes, I have cucumber-junit included. I cannot run my cucumber tests as junit tests from the IDE. Adding @IntegrationTest to my CucumberTest class and running that class as junit did not solve my problems. Commented Jun 9, 2015 at 13:44
  • have you tried configurating the context for the test? @ContextConfiguration(classes = YourAppConfig.class, loader = SpringApplicationContextLoader.class) together with the @IntegrationTest can you run your application through your IDE or isnt that working either? Commented Jun 11, 2015 at 9:28

5 Answers 5

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
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you! One important thing I missed to get this annotations work is presence of info.cukes:cucumber-spring in classpath. Didn't noticed that and struggled a little bit trying to run it.
From Spring Boot 1.2.1 you can use @WebIntegrationTest instead of @WebAppConfiguration and @IntegrationTest.
the repository code does not work. Tests fail when running ./gradlew clean build
@MartínZaragoza I'm not surprised it no longer works, the question is over 3 years old.
7

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

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

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;

}

Update: SpringBoot 1.5.1

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

1 Comment

I've the same problem and the only thing that works for me searching in other sites is this Update for SpringBoot 1.5.1
1

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

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

Comments

0

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.

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

2 Comments

I don't know if this will work any better because this still won't start my application.
@JakeSchwartz OK, that's all I have! Good Luck!
0

First, you'll need to ensure that you have applied spring-boot in gradle. Invoke gradle build which 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 down and runs 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);
     }
}

3 Comments

So you are saying build the jar, run the jar and then run the tests in another thread?
I think I understand what you are saying but I don't think it will help me. I guess I should go back and edit my original question. I have no problem running cucumber tests. I need to be able to run them through gradle so that my CI server can pass my build.
@hd1 How you access this from static method?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.