Search code examples
spring-bootcucumberbddcucumber-jvmcucumber-java

Spring Boot Test with Cucumber and Mockito


I recently started to use Cucumber to implement a set of behavior driven tests for a Java project based on Spring Boot. I would like to call REST endpoints of this application using something like REST Assured or a custom REST client and for external systems and database I would like to setup some mock with Mockito, as I have already done with unit tests.

But I didn't find a complete working solution that I can apply to use Mockito beans in my Cucumber steps, for example to simulate a possible response from database queries.

I found a lot of posts of people over the years that had similar problems with different versions of Cucumber/Junit/Spring but I don't understand if it exists a right way to make these tools working together because I didn't a single complete example related to these tools together. Can anyone share experiences (versions/examples) in real world projects using Spring Boot Test, Cucumber and Mockito?


Solution

  • I finally found the solution. You can start the Cucumber test suite with a class like this

    import static io.cucumber.junit.platform.engine.Constants.PLUGIN_PROPERTY_NAME;
    import static io.cucumber.junit.platform.engine.Constants.GLUE_PROPERTY_NAME;
    
    import org.junit.platform.suite.api.ConfigurationParameter;
    import org.junit.platform.suite.api.IncludeEngines;
    import org.junit.platform.suite.api.SelectClasspathResource;
    import org.junit.platform.suite.api.Suite;
    
    @Suite
    @IncludeEngines("cucumber")
    @SelectClasspathResource("bdd")
    @ConfigurationParameter(key = PLUGIN_PROPERTY_NAME, 
                            value = "pretty")
    @ConfigurationParameter(key = PLUGIN_PROPERTY_NAME, 
                            value = "usage")
    @ConfigurationParameter(key = PLUGIN_PROPERTY_NAME, 
                            value = "html:target/cucumber-reports")
    @ConfigurationParameter(key = GLUE_PROPERTY_NAME, 
                            value = "myproject.cucumber.glue")
    public class RunCucumberTest {
    
    }
    

    In the glue folder you can create a Spring Boot Test that will create the Spring Context and create the bridge between Spring Boot and Cucumber worlds thanks to CucumberContextConfiguration

    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.boot.test.mock.mockito.MockBean;
    import org.springframework.test.context.ActiveProfiles;
    import org.springframework.test.context.TestPropertySource;
    
    import io.cucumber.spring.CucumberContextConfiguration;
    import myproject.repository.MyEntityRepository;
    
    @CucumberContextConfiguration
    @SpringBootTest
    @ActiveProfiles("test")
    public class SpringBootTestStarter {
    
    @MockBean
    private MyEntityRepository myEntityRepository;
    
    }
    

    Here you can use Mockito MockBean annotation and in the steps related to Cucumber scenarios you can now use this bean as a mock