Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

212
Visualizações
How to activate spring boot profile with cucumber

I am looking for a nice way to active a spring profile for my cucumber tests. The cucumber tests need to use a stubbed version of a service which is marked with:

@Profile("test")
@Component
class FooServiceStub extends FooService {...}

The regular service looks like this:

@Profile("prod")
@Component
class FooService {...}    

My requirements:

  • Run cucumber tests with mvn: $ mvn test
  • Run cucumber tests in IDE
  • Run cucumber tests on build server
  • No need to use the -Dspring.profiles.active=... argument

Sources I've found but don't solve my issue:

  • http://www.baeldung.com/cucumber-spring-integration (using @ContextConfiguration loader with SpringApplicationContextLoader.class which is not present in the latest version of Spring Boot, 1.5.2.RELEASE at the time of writing.)
  • programatically set Spring profile in Cucumber (messing with system property)
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Update October 2021:

The below answer is outdated and no longer the accepted answer. Please check the actual accepted answer instead!


Original answer:

I've solved this issue with an annotation that I put on my FeatureStep class.

The annotation:

Note the @ActiveProfiles on it.

import java.lang.annotation.*;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@ContextConfiguration
@ActiveProfiles("test")
@SpringBootTest(
    webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, 
    classes = FeatureTestConfiguration.class)
public @interface FeatureFileSteps {
}

The configuration class is very basic:

@Configuration
@Import(FooApplication.class)
public class FeatureTestConfiguration {

}

Using the annotation:

Adding the annotation to the feature steps:

@FeatureFileSteps
public class FooFeatureSteps {
    @Given(...)
    @When(...)
    @Then(...)
}

Now when running the Cucumber feature tests, either from my IDE, from the command line with maven or on the build server, my test is using the FooServiceSTub and my tests pass.

over 4 years ago · Santiago Trujillo Relatório

0

In more recent cucumber version (6.11.0) you need a class with spring configuration and you have to annotate that one, not the steps definition

@ActiveProfiles("test")
@CucumberContextConfiguration
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class CucumberSpringConfiguration {
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda