2

I am setting up test framework using spring boot , serenity cucumber bdd application.properties file as below

url.lcs="http://xxx.xx.xxx:8090/api/vcs"


Here is my Config class


import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties(prefix = "url")
public class ServerConfig {


    public  String lcs;

    public String getLcs() {
        return lcs;
    }

    public void setLcs(String lcs) {
        this.lcs = lcs;
    }
}

The I created the Base Test where I want use the all the configurations from ServerConfig class

Below is the sample BaseTest Class

import com.mpgs.rscdcf_tf.configuration.ServerConfig;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Component;


@Component
public class BaseTest {
    @Autowired
    private ServerConfig serverConfig;

    public void printServerConfig()
    {

        System.out.println(serverConfig.getLcs());
    }


}

Then I created my step def class as below


import com.mpgs.rscdcf_tf.BaseTest;
import io.cucumber.java.en.Then;
import io.cucumber.java8.En;
import net.thucydides.core.annotations.Step;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;



@SpringBootTest
public class MyStepdefs  implements  En {

    @Autowired
    BaseTest baseTest;

    public MyStepdefs() {


        Given("^enter username and press login button$", () -> {
            System.out.println("login");
        });
        Then("^user is logged in to the system$", () -> {
            System.out.println("system");
        });
        When("^login to the system$", () -> {
            System.out.println("logged logged");
        });
        Then("^show how are you$", () -> {
            System.out.println("I am good thank you");
        });

        }


    @Step("print hello")
    @Then("^hello")
    public void hello(){
        System.out.println("hello");
        baseTest.printServerConfig();

        System.out.println();
        String base = "http://10.157.145.103:8090/api/lcs";
        String url = "/generate/gen4/02_test_suite/02_Test_void_transaction/summary";






    }

when I call the baseTest.printServerConfig(); I expected the value from properties file will be printed but it is always throwing java.lang.NullPointerException

Below is my test runner class

import io.cucumber.junit.CucumberOptions;
import net.serenitybdd.cucumber.CucumberWithSerenity;
import org.junit.runner.RunWith;



@RunWith(CucumberWithSerenity.class)
@CucumberOptions(features = "src/test/resources/features/first/FirstTest.feature",
glue = {"com.mpgs.rscdcf_tf"},  plugin = {"pretty"})
public class TestRunTest {


}

Below is the sample cucumber feature


Feature:  Enter feature name here
   Enter feature description here


  Scenario:  Enter scenario name here
    When login to the system
    And enter username and press login button
    Then show how are you
    Then hello

when I run using unit test I can see the properties file value is pick up but not when i run Serenity. Below is the unit test which is working as expected

import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;


@SpringBootTest
@RunWith(SpringRunner.class)
class BaseTestTest {

    @Autowired
    BaseTest baseTest;


    @Test
    public void print(){

        baseTest.printServerConfig();
    }

}

below is my pom.xml file

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.mpgs</groupId>
    <artifactId>RSCDCF_TF</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>RSCDCF_TF</name>
    <description>RSCDCF_TF</description>
    <packaging>jar</packaging>
    <properties>
        <java.version>1.8</java.version>
        <serenity.version>3.2.4</serenity.version>
        <junit5.version>5.8.2</junit5.version>
        <rest.assured.version>5.1.1</rest.assured.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <encoding>UTF-8</encoding>
        <tags></tags>
        <webdriver.base.url></webdriver.base.url>
    </properties>
    <dependencies>
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>${rest.assured.version}</version>
            <scope>test</scope>
            <exclusions>
                <!-- Exclude Groovy because of classpath issue -->
                <exclusion>
                    <groupId>org.apache.groovy</groupId>
                    <artifactId>groovy</artifactId>
                </exclusion>
                <exclusion>
                    <artifactId>groovy-xml</artifactId>
                    <groupId>org.apache.groovy</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>net.serenity-bdd</groupId>
            <artifactId>serenity-rest-assured</artifactId>
            <exclusions>
                <!-- Exclude Groovy because of classpath issue -->
                <exclusion>
                    <groupId>org.apache.groovy</groupId>
                    <artifactId>groovy</artifactId>
                </exclusion>
                <exclusion>
                    <artifactId>groovy-xml</artifactId>
                    <groupId>org.apache.groovy</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>groovy-json</artifactId>
                    <groupId>org.apache.groovy</groupId>
                </exclusion>
            </exclusions>
            <version>${serenity.version}</version>
        </dependency>
        <dependency>

            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>net.serenity-bdd</groupId>
            <artifactId>serenity-core</artifactId>
            <version>${serenity.version}</version>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>io.cucumber</groupId>
                    <artifactId>cucumber-core</artifactId>
                </exclusion>
                <exclusion>
                    <artifactId>groovy</artifactId>
                    <groupId>org.apache.groovy</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>net.serenity-bdd</groupId>
            <artifactId>serenity-junit5</artifactId>
            <version>${serenity.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.11</version>
        </dependency>

        <dependency>
            <groupId>net.serenity-bdd</groupId>
            <artifactId>serenity-cucumber</artifactId>
            <version>${serenity.version}</version>
            <scope>test</scope>
        </dependency>
        <!-- JUNIT 5 DEPENDENCY-->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit5.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit5.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.assertj</groupId>
            <artifactId>assertj-core</artifactId>
            <version>3.22.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java8</artifactId>
            <version>7.0.0</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>


        <!--        <dependency>-->
<!--            <groupId>io.cucumber</groupId>-->
<!--            <artifactId>cucumber-junit</artifactId>-->
<!--            <version>7.2.3</version>-->
<!--            <scope>compile</scope>-->
<!--        </dependency>-->


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M5</version>
                <configuration>
                    <testFailureIgnore>true</testFailureIgnore>
                    <skip>true</skip>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>3.0.0-M5</version>
                <configuration>
                    <includes>
                        <include>**/*Test.java</include>
                        <include>**/Test*.java</include>
                        <include>**/*TestSuite.java</include>
                        <include>**/When*.java</include>
                    </includes>
                    <parallel>classes</parallel>
                    <threadCount>2</threadCount>
                    <systemPropertyVariables>
                        <webdriver.base.url>${webdriver.base.url}</webdriver.base.url>
                        <junit.jupiter.extensions.autodetection.enabled>true</junit.jupiter.extensions.autodetection.enabled>
                    </systemPropertyVariables>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>net.serenity-bdd.maven.plugins</groupId>
                <artifactId>serenity-maven-plugin</artifactId>
                <version>${serenity.version}</version>
                <dependencies>
                    <dependency>
                        <groupId>net.serenity-bdd</groupId>
                        <artifactId>serenity-single-page-report</artifactId>
                        <version>${serenity.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>net.serenity-bdd</groupId>
                        <artifactId>serenity-navigator-report</artifactId>
                        <version>${serenity.version}</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <tags>${tags}</tags>
                    <reports>single-page-html,navigator</reports>
                </configuration>
                <executions>
                    <execution>
                        <id>serenity-reports</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>aggregate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

Appreciate of any help to resolve the above issue .does @RunWith(SpringRunner.class) vs @RunWith(CucumberWithSerenity.class) makes difference .I tired few solutions as mentioned in other posts but nothing didn't work for me.Not sure what I am missing

Abrar Ahamed
  • 199
  • 11
  • 1
    adding - @Rule public SpringIntegrationMethodRule springIntegrationMethodRule = new SpringIntegrationMethodRule(); in the stepdef class solved the problem – Abrar Ahamed Jul 30 '22 at 06:52

0 Answers0