Java Spring Boot集成测试无法获取默认配置文件,并且在命令行未指定配置文件时引发错误

Java Spring Boot集成测试无法获取默认配置文件,并且在命令行未指定配置文件时引发错误,java,spring,maven,spring-boot,spring-profiles,Java,Spring,Maven,Spring Boot,Spring Profiles,我正在尝试基于springboot配置文件运行基于Selenium的测试。我已经在我的pom中设置了默认配置文件,但是测试没有发现这一点 不确定是否缺少任何配置 命令(执行测试): mvn测试-Dspring.profiles.active=google-工作正常 mvn测试-Dspring.profiles.active=saucelabs-工作正常 mvn测试-错误(如下所示) 错误 Caused by: java.lang.IllegalArgumentException: Could

我正在尝试基于
springboot
配置文件运行基于
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...
src/test/resources/application google.properties

spring.profiles.active=@activatedProperties@
base.url=https://www.google.com
base.url=https://www.saucelabs.com
src/test/resources/application saucelabs.properties

spring.profiles.active=@activatedProperties@
base.url=https://www.google.com
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>

src/测试/资源/
真的
org.springframework.boot
springbootmaven插件

如果未指定任何配置文件,spring将激活默认配置文件。对于默认配置文件,它会扫描
application.properties
文件中的配置值。因此,您需要执行以下操作:

  • 创建
    application.properties
    文件
  • 添加
    base.url=

在那之后,它应该可以正常工作。

我已经有了
application.properties
文件。那么我应该添加
base.url吗=https://www.google.com
在其中?是的,您可以添加它。