0

Hello stackoverflow community, I have been trying to solve this one for many hours to no result, please give me an idea what that might be. If someone interested here is a full project. Trying to run this test, but keep getting NullPointerException.

Test ignored.

Test ignored.

Test ignored.

Test ignored. Failure in before hook:StepDefinition.initializeObjects() Message: java.lang.NullPointerException: Cannot invoke "automation.utils.ConfigurationProperties.getBrowser()" because "this.configurationProperties" is null at automation.glue.StepDefinition.initializeObjects(StepDefinition.java:35)

package automation.glue;

import automation.config.AutomationFrameworkConfiguration;
import automation.drivers.DriverSingleton;
import automation.pages.CheckoutPage;
import automation.pages.HomePage;
import automation.pages.SignInPage;
import automation.utils.ConfigurationProperties;
import automation.utils.Constants;
import cucumber.api.java.Before;
import cucumber.api.java.en.And;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import org.openqa.selenium.WebDriver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;

import static org.junit.Assert.assertEquals;

@ContextConfiguration(classes = AutomationFrameworkConfiguration.class)
public class StepDefinition {
    private WebDriver driver;
    private HomePage homePage;
    private SignInPage signInPage;
    private CheckoutPage checkoutPage;


    @Autowired
    ConfigurationProperties configurationProperties;


    @Before
    public void initializeObjects(){
        DriverSingleton.getInstance(configurationProperties.getBrowser());
        homePage = new HomePage();
        signInPage = new SignInPage();
        checkoutPage = new CheckoutPage();
    }

    @Given("^I go to the Website")
    public void i_go_to_the_Website(){
       // FrameworkProperties frameworkProperties = new FrameworkProperties();
       // DriverSingleton.getInstance(frameworkProperties.getProperty("browser"));
        driver = DriverSingleton.getDriver();
        driver.get(Constants.URL);
    }

    @When("^I click on Sign In button")
    public void i_click_on_sign_in_button(){
       // HomePage homePage = new HomePage();
        homePage.clickSignIn();
    }

    @And("^I specify my credentials and click Login")
    public void i_specify_my_credentials_and_click_login(){
       // SignInPage signInPage = new SignInPage();
       // ConfigurationProperties configurationProperties = new ConfigurationProperties();
        signInPage.logIn(configurationProperties.getEmail(), configurationProperties.getPassword());
    }

    @Then("^I can log into the website")
    public void i_can_log_into_the_website(){
       // HomePage homePage = new HomePage();
       // ConfigurationProperties configurationProperties = new ConfigurationProperties();
        assertEquals(configurationProperties.getUsername(), homePage.getUserName());
    }

}

Compiler points to ConfigurationProperties class, but I can't find anything wrong with it.

package automation.utils;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Component
@PropertySource("framework.properties")
public class ConfigurationProperties {

    @Value("${browser}")
    private String browser;

    @Value("${email}")
    private String email;

    @Value("${password}")
    private String password;

    @Value("${user}")
    private String username;

    public String getBrowser() {return browser;}


    public String getEmail() {return email;}


    public String getPassword() {return password;}


    public String getUsername() {return username;}

}

Here's all I have inside framework.properties file:

 browser=Chrome
 email=laurentiu.raducu@gmail.com
 password=MTIzNDU2
 user=Laurentiu Raducu

RunTests class looks rather simple too:

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@CucumberOptions(
        plugin = {"pretty", "html:target/cucumber-reports"},
        features = "src/main/resources/features"
)
public class RunTests {
    @Test
    public void tests(){}

Any ideas are welcomed. Thanks

dkreminskyi
  • 1
  • 1
  • 1
  • You are using `StepDefinition.configurationProperties` before you assign it, so the value is `null`. You must assign it to something before using it. See [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/q/218384/3744182). – dbc Dec 16 '21 at 07:00
  • 1
    You are only using `@ContextConfiguration` not `@RunWith(SpringRunner.class)` or `@ExtendsWith(SpringExtension)` depending on which JUnit version you are using. So no autowiring will take place as no spring is involved. – M. Deinum Dec 16 '21 at 07:12
  • Have a look at https://www.baeldung.com/cucumber-spring-integration – eol Dec 16 '21 at 08:52

1 Answers1

0

You are using spring cucumber for info cukes, in combination with cucumber io latest release. Make sure you change spring cucumber with the one from Cucumber.io.