当在命令行中未指定配置文件时,Spring Boot 集成测试无法获取默认配置文件并引发错误
Posted
技术标签:
【中文标题】当在命令行中未指定配置文件时,Spring Boot 集成测试无法获取默认配置文件并引发错误【英文标题】: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 【发布时间】:2019-08-27 15:20:24 【问题描述】:我正在尝试基于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$0(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();
【问题讨论】:
【参考方案1】:如果不指定任何配置文件,spring 会激活默认配置文件。对于默认配置文件,它会扫描 application.properties
文件以获取配置值。因此,您需要执行以下操作:
application.properties
文件
添加base.url=<url for default profile>
之后,它应该可以正常工作了。
【讨论】:
我已经有application.properties
文件。那么我应该在其中添加base.url=https://www.google.com
吗?
是的,你可以添加它。【参考方案2】:
您需要启用资源过滤
<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 Boot 集成测试无法获取默认配置文件并引发错误的主要内容,如果未能解决你的问题,请参考以下文章
当在命令行中执行virtualenv venv时报此错误:'utf-8' codec can't decode byte 0xd5 in position 38: invali
spring-boot实战05:Spring Boo多环境配置及配置属性注入到对象