I am trying to run a selenium test (done through GalenFramework) in a spring-boot application. The issue I am facing is in the autowiring part. I am not able to autowire the AppDetails class into my Test class because of NULLPOINTER exception.
I am getting the nullpointer exception while trying to run the test alone (either directly from IntelliJ or mvn test -Dtest=TestClassName method)
But I am able to run (mvn spring-boot:run) the application, which is not required even though I tried to run the application to make sure the value from yaml file (shirtuserurl) is reading correctly.
Why I used spring-boot for selenium ?
I want to load values from Application.yml file as the entire system here are set to use yaml file.
Here are the classes.
Application.java class
[D:\workspace\galen-tester\galen-tester\src\main\java\com\shirt\library\galen\Application.java]
package com.shirt.library.galen;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public String getTestName() {
return "some name";
}
}
Test Class
[D:\workspace\galen-tester\galen-tester\src\test\java\com\shirt\library\galen\user\UserTestUI.java]
package com.shirt.library.galen.user;
import com.shirt.library.galen.Application;
import com.shirt.library.galen.components.GalenTestConfig;
import com.shirt.library.galen.components.TestDevice;
import com.shirt.library.galen.models.AppDetails;
import com.galenframework.config.GalenConfig;
import com.galenframework.config.GalenProperty;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.testng.annotations.Test;
import java.io.IOException;
//@ActiveProfiles("test")
@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = Application.class)
public class UserTestUI extends GalenTestConfig {
@Autowired
private AppDetails appDetails; <-- This comes as NULL
@Autowired
String getTestName;
@Test (dataProvider = "devices")
public void test_user_onDevice(TestDevice devices) throws IOException {
GalenConfig.getConfig().setProperty(GalenProperty.GALEN_RANGE_APPROXIMATION,"5");
System.out.println("Full URL : " + appDetails.getFullURL()); <----- NULLPOINTER exception here
loadAppInBrowser("user");
loginAuthWithCredentials();
CheckLayout(devices);
}
AppDetails class
[D:\workspace\galen-tester\galen-commons\src\main\java\com\shirt\library\galen\models\AppDetails.java]
package com.shirt.library.galen.models;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class AppDetails {
@Value("${shirtuserurl}")
private String fullURL;
//@Bean
public String getFullURL() {
return fullURL;
}
}
I am trying to autowire a @Component under src\main\java\com\shirt... from src\test\java\com\shirt is that the problem ?
Let me know if anyone has any idea on it.
Note:
I am using spring-boot version 1.5.10.RELEASE
try to use these annotations: