Search code examples
springjgiven

@BeforeScenario issues in JGiven Stage (with Spring Support)


Enjoying using JGiven with it's Spring support!

However, I'm having issues with @Autowired playing nice with @BeforeScenario in a Stage class (annoted with @JGivenStage. I have a minimal spring configuration (which reads a couple of property files, scans for components etc) and is denoted with @EnableJGiven i.e.

@Configuration
@EnableJGiven
@PropertySource(ignoreResourceNotFound = true,
    value = { "classpath:/config/qa.properties", "classpath:/config/env-${spring.profiles.active:}.properties" })
@ComponentScan(basePackages = { "com.mytest.qa.api", "com.mytest.stage", "com.mytest.qa.ui" })
public class SpringTestConfiguration {}

I'm tyring to create a JGiven scenario which has several methods but only does setup once (as it takes several minutes) inside the Given stage. The feature e.g. MyFeatureTest (names changed for brevity) is as follows: -

@ContextConfiguration(classes = SpringTestConfiguration.class)
public MyFeatureTest extends SpringScenarioTest<MyGiven, MyWhen, MyThen> {

    @Test
    public void analysis_should_be_correct() {
        given().I_am_at_the_home_page_with_example_dataset_analysed();
        when().I_click_on_analysis();
        then().I_expect_the_analysis_should_be_correct();
    }

    @Test
    public void download_should_be_correct() {
        given().I_am_at_the_home_page_with_example_dataset_analysed();
        when().I_click_on_download();
        then().I_expect_the_download_should_be_correct();
    }

}

My MyGiven class is as follows: -

@JGivenStage
public MyGiven extends Stage<MyGiven> {

    @Autowired
    private QaApi qaApi;    // QA API use to hit API directly e.g. for setup

    @Autowired
    private HomePage homePage;

    public MyGiven I_am_at_the_home_page_with_example_dataset_analysed() {
        homePage.visit().login(defaultUsername, defaultPassword);
        return self();
    }

    @BeforeScenario
    private void setupSingleAnalysedUpload() {
        qaApi.uploadAndAnalyseExampleDataSet();              // !!!! qaApi is NULL !!!!
    }
}

What i'm trying to achieve is that when I run a scenario (or several tests of a scenario) that the setup setupSingleAnalysedUpload() method is only called once (as it's very slow).

My approach was to annotate this method with the @BeforeScenario annotation. However, the protected QaApi qaApi; is null (doesn't get initialised via the @Autowired annotation).

Note - If I comment out the setupSingleAnalysedUpload method and stick a breakpoint on homePage.visit().login line then QaApi is initialised without issue - assuming spring life-cycling issues with @BeforeScenario annotation.

Completely stomped - my gut feeling is that there is missing functionality in the jgiven-spring library? If so, what is the best-practice work around?


Solution

  • This should be fixed in v0.14.0