Spring Boot集成测试无法在命令行未指定配置文件时获取默认配置文件并抛出错误
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot集成测试无法在命令行未指定配置文件时获取默认配置文件并抛出错误相关的知识,希望对你有一定的参考价值。
我正在尝试基于Selenium
配置文件运行基于Spring Boot
的测试。我已经在我的pom
中设置了默认配置文件,但是测试没有看到选择它。
不确定我是否缺少任何配置。
命令(执行测试):
- mvn test -Dspring.profiles.active = google - 工作精细
- mvn test -Dspring.profiles.active = saucelabs - 工作精细
- mvn test - 错误(如下所示)
错误
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.hml
<?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 /测试/资源/ application.properties
spring.profiles.active=@activatedProperties@
SRC /测试/资源/ application-google.properties
base.url=https://www.google.com
SRC /测试/资源/ application-saucelabs.properties
base.url=https://www.saucelabs.com
homepage test.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();
}
}
}
答案
如果未指定任何配置文件,则spring会激活默认配置文件。对于默认配置文件,它会扫描application.properties
文件以获取配置值。因此,您需要执行以下操作:
- 创建
application.properties
文件 - 添加
base.url=<url for default profile>
在那之后,它应该工作正常。
另一答案
您需要启用资源过滤
<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集成测试无法在命令行未指定配置文件时获取默认配置文件并抛出错误的主要内容,如果未能解决你的问题,请参考以下文章
无法集成测试 gradle 多模块 Spring-Boot-Application
Spring Boot 集成测试无法访问 application.properties 文件
无法理解 Spring Boot 如何使用 maven 管理集成测试。它是不是使用故障安全插件?