Gunnar Eketrapp
Gunnar Eketrapp

Reputation: 2159

Spring boot cucumber testing

I try to perform some Cucumber tests of a Spring boot application.

It seems like Spring Boot is not started before tests are running.

What am i missing?

https://bitbucket.org/oakstair/spring-boot-cucumber-example

Upvotes: 1

Views: 2507

Answers (3)

Gunnar Eketrapp
Gunnar Eketrapp

Reputation: 2159

I found the problem and has updated the repo.

I did the following to get it working:

  • Added RANDOM_PORT to @SpringBootTest
  • Added @ContextConfiguration
  • Switched from RestTemplate to TestRestTemplate

Upvotes: 1

harsh tibrewal
harsh tibrewal

Reputation: 835

Let's say you have a feature file, feature1, and glueCode in org.xyz.feature1

@RunWith(Cucumber.class)
@CucumberOptions(
    plugin = {"pretty"},
    features = "src/test/java/resources/feature/feature1",
    glue = {"org.xyz.feature1"})
public class CucumberTest {

}

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {Application.class},
                webEnvironment = WebEnvironment.RANDOM_PORT)
@ContextConfiguration
@Ignore
@Transactional
public class FeatureTest extends CucumberTest {

  @LocalServerPort
    int randomServerPort;

  @Given("........")
  public void test_1 {

   }

}

Upvotes: 2

Barath
Barath

Reputation: 5283

My Cucumber repo still runs without doing all the above steps:

https://github.com/BarathArivazhagan/Cucumber-spring-integration

Docs : https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html

To add more:

  1. @SpringBootTest takes care of loading the application context in that case @ContextConfiguration is reductant.

  2. Spring test automatically provides a bean of TestRestTemplate which can be autowired but still it should work with RestTemplate also.

  3. It still runs without RANDOM_PORT but RANDOM port can be also used in conjunction for testing.

Upvotes: 2

Related Questions