Search code examples
javaspring-bootmavenantcucumber

Run a spring-boot application and its cucumber tests in the same project using one maven "command"


Overview

A maven project using spring boot for which some cucumber tests are implemented (in the same project!).

src
 |
 |-main
 |  |
 |  |-java
 |     |
 |     |-SpringBootApp
 |     |-Controller
 |
 |-test
   |
   |-java
   |  |
   |  |-cucumbertests
   |       |     
   |       |-StepDefinitions
   |       |-CucumberTestsRunner
   |
   |-resources
        |-features
              |-hello.feature

Controller

@RestController
@RequestMapping("/")
public class Controller {

    @GetMapping("hello")
    public String hello() {
        return "Hello!";
    }
}

CucumberTestsRunner

@RunWith(Cucumber.class)
@CucumberOptions(glue = "cucumbertests")
pulic class CucumberTestsRunner {
}

StepDefinitions

public class StepDefinitions {

    private String response;

    @When("I say hello")
    public void iSayHello() {
        // rest assured call
        response = get("<base_url>/hello").extract().asString();
    }

    @Then
    public void iMGreetedWithHello() {
        assertEquals("Hello!", response);
    }
}

With this in place,

  • I can run in a console mvn spring-boot:run (from where the SpringBoot application starts)
  • and then in another console mvn test -Dtest=CucumberTestsRunner, from where the Cucumber tests run against the webservices

So far so good, the tests pass without any issue.

Problem

I'd like to be able to issue a single command to start the SpringBoot application and then run the tests against the started application. Then after the tests finished, kill the SpringBoot application.

Ideally this is intended to be used in a CI system like Jenkins.

I was exploring the Apache Maven AntRun plugin as an option, but this is my first time doing this kind of set up and I'm not sure this would be a right approach. Previously I have seen set up like this but on independent projects (tests in a different app than the tested application).


Solution

  • Instead of using spring-boot:run in one console and running your tests in a second one, you can use use spring-boot:start and spring-boot:stop.

    Something like mvn spring-boot:start test spring-boot:stop would start the application, run the tests and then stop the application again.