Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to access @Autowired classes inside @Steps class #3586

Open
iamtech123 opened this issue Dec 11, 2024 · 5 comments
Open

Unable to access @Autowired classes inside @Steps class #3586

iamtech123 opened this issue Dec 11, 2024 · 5 comments

Comments

@iamtech123
Copy link

iamtech123 commented Dec 11, 2024

What happened?

@autowire not initiating the class inside @steps class with Junit 5 - @IncludeEngines("cucumber")

What did you expect to happen?

@autowire class should be accessible inside @steps classes

Serenity BDD version

4.0.28

JDK version

17

Execution environment

OS: Windows
Browser : Chrome
Framework :
Spring boot - 3.4.0
Junit 5
Cucumber - 7.15.0

How to reproduce the bug.

Runner class
@suite
@IncludeEngines("cucumber")
@SelectClasspathResource("/features")
@ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "com.automation.springboot.cucumber.steps")
public class TestRunner {
}

Init class
@SpringBootTest(classes = CucumberTestApp.class)
public class Init extends PageObject {

@Autowired
RunConfig runConfig;

}

Run config

@DaTa
@component
@propertysource("classpath:config/newprop.properties")
public class RunConfig {

@Value("${app_url}")
private String strApp_URL;

}

Step Def

@component
public class DemoStepDef extends Init{

@Autowired
RunConfig runConfig;

@Steps
GooglePage googlePage;

@Given("Sample test")
public void demoStep(){
    System.out.println("In the test case-----");
    System.out.println("App URL : "+runConfig.getStrApp_URL());
    googlePage.openApp();
}

@Then("search with {string}")
public void searchWith(String arg0) {
    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
    googlePage.search(arg0);
}

}

Google page

@component
public class GooglePage extends PageObject {

@Autowired
RunConfig runConfig;

@Step
public void openApp(){
    getDriver().get(runConfig.getStrApp_URL());

}

@Step
public void search(String strKeyword){
    find(By.name("q")).sendKeys(strKeyword);
}

}

RunConfig works find in Step definition class but returns null in GooglePage class. How to inject Spring boot context inside @steps classes?

How can we make it happen?

Add it to the Serenity BDD backlog and wait for a volunteer to pick it up

@iamtech123
Copy link
Author

@wakaleo could you please look into it?

@iamtech123 iamtech123 changed the title @Autowire not initiating the class inside @Steps class Unable to access @Autowired classes inside @Steps class Dec 17, 2024
@wakaleo
Copy link
Member

wakaleo commented Jan 2, 2025

@wakaleo could you please look into it?

This is most likely a configuration issue in your test setup. I work on many client projects using Spring and SpringBoot with Serenity, but the annotations can be tricky to get right so that they are picked up correctly. I would be happy to take a look as part of a commercial support package.

@iamtech123
Copy link
Author

Hi @wakaleo, is there any sample project i could refer?

@wakaleo
Copy link
Member

wakaleo commented Jan 8, 2025

Have a look at https://github.com/serenity-dojo/wordle

@iamtech123
Copy link
Author

Thank you @wakaleo .. I will look into it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants