3

My cucumber tests get the spring boot context refreshed on each pass, I have some database cache being done and it is killing the performance of the compilation process.

My abstract test is marked as @SpringBootTest(defined_port) with a @ContextConfiguration(loader=SpringBootContextLoader.class).

I already tried adding DirtiesContext but it did not work... any ideas?

Chuck
  • 235
  • 3
  • 9

1 Answers1

3

If you are using the latest Cucumber (v5.7.0) to make Cucumber aware of your test configuration you can annotate a configuration class on your glue path with @CucumberContextConfiguration and with one of the following annotations: @ContextConfiguration, @ContextHierarchy or @BootstrapWith. If you are using SpringBoot, you can annotate configuration class with @SpringBootTest

For example:

import com.example.app;
import org.springframework.boot.test.context.SpringBootTest;
import io.cucumber.spring.CucumberContextConfiguration; 

@CucumberContextConfiguration 
@SpringBootTest(classes = TestConfig.class) public class 
CucumberSpringConfiguration { }

You can then @Autowire components from the application context into any step definition file. No further spring configuration is needed. For example:

package com.example.app;

public class MyStepDefinitions { 

 @Autowired
 private MyService myService;

 @Given("feed back is requested from my service")
 public void feed_back_is_requested(){ 
    myService.requestFeedBack(); 
  }
}

The only requirement is that both MyStepDefinitions and CucumberSpringConfiguration are both in a package that is on the glue path. So either you have configured @CucumberOptions(glue="com.example") explicitly or your test runner class is in the same package as your step definition (com.example).

You can find more information in the cucumber-spring module in github. https://github.com/cucumber/cucumber-jvm/tree/main/cucumber-spring

Dmytro Chasovskyi
  • 3,209
  • 4
  • 40
  • 82
M.P. Korstanje
  • 10,426
  • 3
  • 36
  • 58
  • that's actually interesting. so you are scanning the entire classpath for `CucumberSpringConfiguration` annotated classes, correct? I mean I see no other way to do this. It's also interesting, how this glue is later processed, would take a look at the code if you are willing to share a hint. thank you. – Eugene Sep 10 '21 at 02:13