当前位置: 首页 > 知识库问答 >
问题:

maven配置文件覆盖测试资源

傅奕
2023-03-14

我正在尝试制作maven配置文件,它将使用两种不同的DBMS。DBMS配置存储在maven配置文件中。Web应用程序从src/main/resources中的文件connection.properties获取设置。在src/test/resources中还有一个标题相同的类似文件connection.properties,该文件只应在测试lyfecycle Maven期间上传。然后spring core使用connection.properties中指定的DBMS连接设置。

我对maven配置文件有问题,该配置文件在测试生命周期maven运行时覆盖了来自测试目录的src/main/resources/connection.properties上的资源,如src/test/resources/connection.properties。

<profile>
        <id>profile-postgres</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <database.driver_class_name>org.postgresql.Driver</database.driver_class_name>
            <database.url>jdbc:postgresql://127.0.0.1:5432/bulls_and_cows</database.url>
            <database.username>postgres</database.username>
            <database.password>postgres</database.password>

            <jpa.show_sql>true</jpa.show_sql>
            <jpa.generate_ddl>true</jpa.generate_ddl>
            <jpa.database>POSTGRESQL</jpa.database>
            <jpa.database_platform>org.hibernate.dialect.PostgreSQL95Dialect</jpa.database_platform>
            <jpa.hibernate.hbm2ddl.auto>validate</jpa.hibernate.hbm2ddl.auto>
            <jpa.hibernate.format_sql>false</jpa.hibernate.format_sql>

            <h2.scope>test</h2.scope>
        <dependencies>
            <!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
            <dependency>
                <groupId>org.postgresql</groupId>
                <artifactId>postgresql</artifactId>
                <version>42.2.1</version>
            </dependency>
        </dependencies>
        </properties>
    </profile>
    <profile>
            <id>profile-h2</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <properties>
                <database.driver_class_name>org.h2.Driver</database.driver_class_name>
                <database.url>jdbc:h2:mem:h2db;DB_CLOSE_DELAY=-1</database.url>
                <database.username>sa</database.username>
                <database.password>sa</database.password>

                <jpa.show_sql>true</jpa.show_sql>
                <jpa.generate_ddl>true</jpa.generate_ddl>
                <jpa.database>H2</jpa.database>
                <jpa.database_platform>org.hibernate.dialect.H2Dialect</jpa.database_platform>
                <jpa.hibernate.hbm2ddl.auto>create-drop</jpa.hibernate.hbm2ddl.auto>
                <jpa.hibernate.format_sql>false</jpa.hibernate.format_sql>

                <h2.scope>compile</h2.scope>
            </properties>
        </profile>
</profiles>

此配置文件覆盖src/main/resources上的src/test/resources中的my Connection.properties。

来自src/test/resources的

database.driver_class_name=org.h2.Driver
database.url=jdbc:h2:mem:h2db;DB_CLOSE_DELAY=-1
database.username=sa
database.password=sa

来自src/main/resources的

database.driver_class_name=${database.driver_class_name}
database.url=${database.url}
database.username=${database.username}
database.password=${database.password}
<testResources>
    <testResource>
        <directory>${project.basedir}/src/test/resources</directory>
        <filtering>true</filtering>
    </testResource>
</testResources>

但是在Maven的测试生命周期中总是使用src/main/resources中的connection.properties。

我使用https://travis-ci.org/wedism/bullsandcows/builds/449051809的配置文件进行的旧版本失败了。

My repo with master branch https://github.com/wedism/bullsandcows/blob/master/pom.xml。

我的回购with with_profiles_h2_postgres分支https://github.com/wedism/bullsandcows/blob/with_profiles_h2_postgres/pom.xml

Profile profile-postgres应该是main,如activeByDefault=true

共有1个答案

徐秋月
2023-03-14

为了修复此问题,我将 等属性名称更改为

 类似资料:
  • 问题内容: 请参阅以下Maven生成的项目 我想 在运行测试 时使用test / resources / datasource-settings.xml中的设置,而不要使用main / resources / datasource-settings.xml中的设置。可能吗 ?如果是这样,我应该怎么做才能达到目标? 问题答案: 放置在(默认位置)中的资源将自动添加到Maven为您的单元测试设置的类路

  • 11.3. 测试覆盖率 就其性质而言,测试不可能是完整的。计算机科学家Edsger Dijkstra曾说过:“测试能证明缺陷存在,而无法证明没有缺陷。”再多的测试也不能证明一个程序没有BUG。在最好的情况下,测试可以增强我们的信心:代码在很多重要场景下是可以正常工作的。 对待测程序执行的测试的程度称为测试的覆盖率。测试覆盖率并不能量化——即使最简单的程序的动态也是难以精确测量的——但是有启发式方法

  • 我有一个基本的maven多模块,父模块是POM,其子模块是domain、service和Web。 我已经在pom父级中添加了jacoco插件和所需的配置,以便将测试覆盖率报告附加到一个Sonar很容易找到的文件中。 我的问题是,Sonar只显示第一个模块的测试覆盖范围(是domain)(即使我用编辑器打开文件,看到其他模块的类名被追加在其中)。 这种配置会有什么问题? 为了它的价值,声纳分析被称为

  • 我正在尝试使用maven配置文件实例化hibernate配置文件。我的配置文件位于下,在pom文件中,我标记了该文件夹以用于资源过滤。有趣的是,我可以从属性文件中加载相同的属性,而hibernate在解析配置文件时抛出异常。这是我正在使用的代码示例。 POM-资源筛选 文件结构 资源hibernate.cfg.xml

  • 决策覆盖技术属于白盒测试,它为布尔值提供决策覆盖。此技术报告布尔表达式的真/假结果。每当语句中有两个或多个结果的可能性时,如while语句,if语句和case语句(控制流语句),它被视为决策点,因为有两个结果为或。 决策覆盖率通过使用控制流图或图表涵盖代码的每个布尔条件的所有可能结果。 通常,决策点有两个决策值,一个是,另一个是,这就是为什么大多数时候结果总数是2的原因。决策覆盖率的百分比可以通过

  • 语句覆盖是广泛使用的软件测试之一。它来自白盒测试。 语句覆盖技术用于设计白盒测试用例。该技术涉及至少执行一次源代码的所有语句。它用于计算源代码中源代码中执行的语句总数。 语句覆盖在白盒测试过程中得出测试用例的场景,该过程基于代码的结构。 在白盒测试中,测试人员的专注点是内部源代码和流程图或代码流程图的工作。 通常,在内部源代码中,有各种各样的元素,如运算符,方法,数组,循环,控制语句,异常处理程序