当未在命令行中指定配置文件时,Spring Boot集成测试将无法获取默认配置文件并抛出错误

Nital:

我正在尝试Selenium基于Spring Boot配置文件运行基础测试我已经在我的个人资料中设置了默认配置文件,pom但测试并未看到该配置文件

不知道我是否缺少任何配置。

命令(执行测试):

  • mvn test -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();
        }
    }

}
达山·梅塔(Darshan Mehta):

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

  • 建立application.properties档案
  • base.url=<url for default profile>

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

春天不重写的命令行活动的配置文件

在配置文件中指定JVM选项

集成测试春天开机默认的配置文件

从命令行停用Maven配置文件

在Spring Boot中从命令行设置活动配置文件和配置位置

解析配置文件,环境和命令行参数,以获取单个选项集合

无法在Spring Boot 2.2中设置命令行配置文件

如何将Spring Boot配置文件与Maven配置文件集成?

无法从gradle命令行传递spring配置文件

如何在MSTest命令行中指定.runsettings文件?

通过在命令行上指定多个Maven配置文件来堆叠属性

如何使用指定的弹簧配置文件运行maven命令行?

Spring Boot的多个配置文件可在IDE中使用,但不能从命令行使用

将Spring Boot配置文件与命令行参数一起使用

如何使用Applescript(或命令行)获取已加载的显示器颜色配置文件?

当通过命令行参数从配置文件中指定数组的大小时,如何使用std :: array?

如何使用指定的配置文件从命令行vs2019 for C ++?

Snakemake在命令行上从嵌套的config.yaml文件中指定配置参数

为Java命令行运行指定特定的配置文件

如何在命令行中使用指定的配置文件在OSX中启动新的终端?

Linux,无主目录安装时命令行程序的配置文件

lsyncd错误:命令行中只能有一个配置文件。的Ubuntu

通过命令行将.reg文件导入默认用户配置文件

如何使用命令行编辑配置文件?

从命令行到 libreoffice 配置文件的路径

在 webpack 配置中指定 --json 而不是命令行

从命令行调用snowsql时将配置文件作为arg传递

在 Windows PowerShell 配置文件中添加 Starship 命令行时出错

命令行選項的配置文件