1

i have properties named application-QA1.properties

#Endpoint
web.loginPage=https://www.phptravels.net/

#login data
common.data.username=tes
common.data.password=tes

#Default Param
param.storeId=10001
param.channelId=tes-web
param.clientId=10001
param.requestId=RANDOM
param.businessChannel=web
param.requestParams={}
param.test=true

and i put it in:

src/test/resources

and i create class as instance for that properties named WebProperties.java

package com.project.automation.ui.properties;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@Component("com.project.automation.ui.properties.WebProperties")
@ConfigurationProperties(prefix = "web")
public class WebProperties {
    private String loginPage;
}

and i put it in:

src/main/java/mypackage/properties

and i want to access that class variable on LoginPage.java

package com.project.automation.ui.pages;

import com.project.automation.ui.models.xpath.CommonXpathModel;
import com.project.automation.ui.properties.CommonProperties;
import com.project.automation.ui.properties.WebProperties;
import lombok.Data;
import net.serenitybdd.core.pages.PageObject;
import org.openqa.selenium.By;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Data
@Component("com.project.automation.ui.pages.LoginPage")
public class LoginPage extends PageObject {

  @Autowired
  CommonPage commonPage;

  @Autowired
  CommonXpathModel commonXpathModel;

  @Autowired
  WebProperties webProperties;

  @Autowired
  CommonProperties commonProperties;

  public void openPage(){
    //on this code i want to get that properties value through its class instance
    getDriver().get(webProperties.getLoginPage());
  }
}

the page i put it in:

src/main/java/mypackage/pages

and this is my pom.xml located in root of my project.

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.project.automation.ui</groupId>
    <artifactId>wina</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <serenity.version>1.9.45</serenity.version>
        <maven.failsafe.plugin>2.22.0</maven.failsafe.plugin>
        <selenium.version>3.14.0</selenium.version>
        <buildDirectory>${project.basedir}/target</buildDirectory>
        <serenity.testlink.integration.version>3.4.5</serenity.testlink.integration.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mustache</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>${selenium.version}</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-api</artifactId>
            <version>${selenium.version}</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-server</artifactId>
            <version>${selenium.version}</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-chrome-driver</artifactId>
            <version>${selenium.version}</version>
        </dependency>
        <dependency>
            <groupId>com.googlecode.lambdaj</groupId>
            <artifactId>lambdaj</artifactId>
            <version>2.3.3</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.8</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>net.serenity-bdd</groupId>
            <artifactId>serenity-core</artifactId>
            <version>${serenity.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>net.serenity-bdd</groupId>
            <artifactId>serenity-cucumber</artifactId>
            <version>1.9.45</version>
        </dependency>
        <dependency>
            <groupId>net.serenity-bdd</groupId>
            <artifactId>serenity-spring</artifactId>
            <version>${serenity.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>net.serenity-bdd</groupId>
            <artifactId>serenity-rest-assured</artifactId>
            <version>${serenity.version}</version>
        </dependency>
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>3.0.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <directory>${buildDirectory}</directory>
        <testOutputDirectory>${buildDirectory}/test-classes</testOutputDirectory>
        <outputDirectory>${buildDirectory}/classes</outputDirectory>
        <resources>
            <resource>
                <directory>src/test/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*.properties</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>test-jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>${maven.failsafe.plugin}</version>
                <executions>
                    <execution>
                        <id>integration-test</id>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <includes>
                        <include>**/CucumberRunner.java</include>
                    </includes>
                    <systemPropertyVariables>
                        <cucumber.options>--junit,--step-notifications</cucumber.options>
                        <spring.config.location>classpath:/application-QA1.properties</spring.config.location>
                    </systemPropertyVariables>
                </configuration>
            </plugin>
            <plugin>
                <groupId>net.serenity-bdd.maven.plugins</groupId>
                <artifactId>serenity-maven-plugin</artifactId>
                <version>${serenity.version}</version>
                <executions>
                    <execution>
                        <id>serenity-reports</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>aggregate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

but it keeps return null.

please help me, i already read many tutorial but still no luck, beside i am beginner at spring boot. thank you

2 Answers2

1

The problem is naming convention of properties file, Since you named it as application-QA1.properties it becomes profiles specific properties file Profile-specific properties and you can load those properties by making profile active in application.properties like here

spring.profiles.active=QA1

Or through command line

java -jar myproject.jar --spring.profiles.active=QA1

And another information, value in @Component annotation indicates the bean name

he value may indicate a suggestion for a logical component name, to be turned into a Spring bean in case of an autodetected component.

Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
  • thank you for the reply, but the things is my project also cant read application.properties value. so the main problem was cannot read properties value, maybe i put the file wrong. i put all properties file in src/test/resources. maybe i should put that file inside src/main/resources ? – Ahmad Leo Yudanto Nov 14 '19 at 19:28
  • Yes that's correct you have to move them to 'src/main/resources' @AhmadLeoYudanto – Ryuzaki L Nov 14 '19 at 19:34
  • yes, i already moved that while ago. but still no luck, maybe i need to mvn clean or something? the properties now moved to target/classes after i run the project via cucumberRunner.java (before the properties belongs in target/test-classes ) – Ahmad Leo Yudanto Nov 14 '19 at 19:40
  • sorry i think i'm confused, is this for test cases ? or for main application @AhmadLeoYudanto ? – Ryuzaki L Nov 14 '19 at 19:47
  • sorry to make you confused, i just want to read the properties values that it. but i found new problem, even when i set the value of loginPage variable not using application-QA1.properties values it still return null. so i think its not read properties value problem but autowired and component things. sorry. – Ahmad Leo Yudanto Nov 14 '19 at 19:57
0

You indicated that the location of your properties file is:

<spring.config.location>classpath:/application-QA1.properties</spring.config.location>

But you put the properties file inside a package directory...

src/main/java/mypackage/properties

So when you build the project, the properties file is not going to end up in the root of your classpath... It is going to end up in the same package directory of your classpath. See for yourself.

LowKeyEnergy
  • 652
  • 4
  • 11
  • thank you for the reply, but i put my properties file inside src/test/resources. and properties class inside src/main/java/mypackage/properties. – Ahmad Leo Yudanto Nov 14 '19 at 18:30