Spring 当未在命令行指定配置文件时,启动集成测试无法获取默认配置文件并抛出错误
Spring Boot integration test not able to pick up the default profile and throwing error when the profile is not specified at the command line
我正在尝试 运行 基于 Spring Boot
配置文件的 Selenium
测试。我已经在我的 pom
中设置了默认配置文件,但测试没有发现它。
不确定我是否缺少任何配置。
命令(执行测试):
- mvn 测试 -Dspring.profiles.active=google - 工作正常
- mvn 测试 -Dspring.profiles.active=saucelabs - 工作正常
- mvn 测试 - 错误(如下所示)
错误
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'base.url' in value "${base.url}"
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:172) ~[spring-core-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:124) ~[spring-core-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:237) ~[spring-core-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:211) ~[spring-core-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.lambda$processProperties[=11=](PropertySourcesPlaceholderConfigurer.java:175) ~[spring-context-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:839) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1083) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1062) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:581) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:370) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
... 31 common frames omitted
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 9.868 s <<< FAILURE! - in com.example.demo.HomePageTest
[ERROR] loadHomePage(com.example.demo.HomePageTest) Time elapsed: 0.001 s <<< ERROR!
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.example.demo.HomePageTest': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'base.url' in v
alue "${base.url}"
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'base.url' in value "${base.url}"
2019-04-05 12:40:55.541 INFO 15512 --- [ Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@66ea810: startup date [Fri Apr 05 12:40:44 EDT 2019]; root of context hierarchy
[INFO]
[INFO] Results:
[INFO]
[ERROR] Errors:
[ERROR] HomePageTest.loadHomePage » BeanCreation Error creating bean with name 'com.ex...
pom.xml
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>springboot-profile-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-profile-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>2.45.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>google</id>
<properties>
<activatedProperties>google</activatedProperties>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>uat</id>
<properties>
<activatedProperties>saucelabs</activatedProperties>
</properties>
</profile>
</profiles>
</project>
src/test/resources/application.properties
spring.profiles.active=@activatedProperties@
src/test/resources/application-google.properties
base.url=https://www.google.com
src/test/resources/application-saucelabs.properties
base.url=https://www.saucelabs.com
HomePageTest.java
@RunWith(SpringRunner.class)
@SpringBootTest
public class HomePageTest {
private static final String CHROME_DRIVER_EXE = "chromedriver.exe";
private static WebDriver browser;
@Value("${base.url}")
private String baseUrl;
@BeforeClass
public static void init() {
//load driver
String filePath = ClassLoader.getSystemClassLoader().getResource(CHROME_DRIVER_EXE).getFile();
System.setProperty("webdriver.chrome.driver", filePath);
//init driver
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("useAutomationExtension", false);
browser = new ChromeDriver(options);
}
@Test
public void loadHomePage() {
browser.get(baseUrl);
assertNotNull(browser.getPageSource());
}
@AfterClass
public static void tearDown() {
if (browser != null) {
browser.close();
browser.quit();
}
}
}
您需要启用资源过滤
<build>
<resources>
<resource>
<directory>src/test/resources/</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
如果您没有指定任何配置文件,spring 将激活默认配置文件。对于默认配置文件,它会扫描 application.properties
文件以获取配置值。因此,您需要执行以下操作:
- 创建
application.properties
文件
- 添加
base.url=<url for default profile>
之后,它应该可以正常工作。
我正在尝试 运行 基于 Spring Boot
配置文件的 Selenium
测试。我已经在我的 pom
中设置了默认配置文件,但测试没有发现它。
不确定我是否缺少任何配置。
命令(执行测试):
- mvn 测试 -Dspring.profiles.active=google - 工作正常
- mvn 测试 -Dspring.profiles.active=saucelabs - 工作正常
- mvn 测试 - 错误(如下所示)
错误
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'base.url' in value "${base.url}"
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:172) ~[spring-core-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:124) ~[spring-core-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:237) ~[spring-core-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:211) ~[spring-core-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.lambda$processProperties[=11=](PropertySourcesPlaceholderConfigurer.java:175) ~[spring-context-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:839) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1083) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1062) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:581) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:370) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
... 31 common frames omitted
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 9.868 s <<< FAILURE! - in com.example.demo.HomePageTest
[ERROR] loadHomePage(com.example.demo.HomePageTest) Time elapsed: 0.001 s <<< ERROR!
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.example.demo.HomePageTest': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'base.url' in v
alue "${base.url}"
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'base.url' in value "${base.url}"
2019-04-05 12:40:55.541 INFO 15512 --- [ Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@66ea810: startup date [Fri Apr 05 12:40:44 EDT 2019]; root of context hierarchy
[INFO]
[INFO] Results:
[INFO]
[ERROR] Errors:
[ERROR] HomePageTest.loadHomePage » BeanCreation Error creating bean with name 'com.ex...
pom.xml
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>springboot-profile-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-profile-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>2.45.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>google</id>
<properties>
<activatedProperties>google</activatedProperties>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>uat</id>
<properties>
<activatedProperties>saucelabs</activatedProperties>
</properties>
</profile>
</profiles>
</project>
src/test/resources/application.properties
spring.profiles.active=@activatedProperties@
src/test/resources/application-google.properties
base.url=https://www.google.com
src/test/resources/application-saucelabs.properties
base.url=https://www.saucelabs.com
HomePageTest.java
@RunWith(SpringRunner.class)
@SpringBootTest
public class HomePageTest {
private static final String CHROME_DRIVER_EXE = "chromedriver.exe";
private static WebDriver browser;
@Value("${base.url}")
private String baseUrl;
@BeforeClass
public static void init() {
//load driver
String filePath = ClassLoader.getSystemClassLoader().getResource(CHROME_DRIVER_EXE).getFile();
System.setProperty("webdriver.chrome.driver", filePath);
//init driver
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("useAutomationExtension", false);
browser = new ChromeDriver(options);
}
@Test
public void loadHomePage() {
browser.get(baseUrl);
assertNotNull(browser.getPageSource());
}
@AfterClass
public static void tearDown() {
if (browser != null) {
browser.close();
browser.quit();
}
}
}
您需要启用资源过滤
<build>
<resources>
<resource>
<directory>src/test/resources/</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
如果您没有指定任何配置文件,spring 将激活默认配置文件。对于默认配置文件,它会扫描 application.properties
文件以获取配置值。因此,您需要执行以下操作:
- 创建
application.properties
文件 - 添加
base.url=<url for default profile>
之后,它应该可以正常工作。