I tried to use property file in my maven-java project for test automation.
This is the context.xml file
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/context ">
<util:properties id="properties" location="classpath:test-context.properties"/>
<context:property-placeholder properties-ref="properties" ignore-unresolvable="false"/>
<bean id="settings" class="util.TestSettings">
<property name="properties" ref="properties"/>
</bean>
These are my java classes.
import java.util.Properties;
public class TestSettings {
private static Properties properties;
public static String getProperty(String key) {
return properties.getProperty(key);
}
public void setProperties(Properties properties) {
TestSettings.properties = properties;
}
}
@ContextConfiguration(locations = "classpath:test-context.xml")
public class P_1_LoginPage extends AbstractTestNGSpringContextTests {
private P_1_LoginPage p1LoginPage;
private WebDriver driver;
public P_1_LoginPage(WebDriver driver) {
this.driver = driver;
driver.get(TestSettings.getProperty("base.url"));
}
@BeforeClass(alwaysRun = true)
public void BeforeTest() throws MalformedURLException {
DesiredCapabilities capability = DesiredCapabilities.firefox();
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
p1LoginPage = new P_1_LoginPage(driver);
}
public void assertThere() {
//assert here
}
update
here is my context.property file.
base.url=http://uname:pword@test.mysite.com.au/sdfdf
email.Queue.url=http://uname:pword@test.mysite.com.au/admin/admin/show_msgs
When I try to run test cases, it gives a null pointer exception here.
public static String getProperty(String key) {
return properties.getProperty(key);
}
Can some one help me to figure out the issue here?
Your code breaks few Spring guidelines, like for example static field used to access properties, or creating
P_1_LoginPagewith new operator outside container. But the main problem for NullPointer is, that Spring context is not yet initialized in@BeforeClass(method should be static also, otherwise it will cause exception). Replace@BeforeClasswith@Before.