我正在尝试为集成测试提出一个并行的 Maven 测试执行。
在测试执行开始之前使用sql-maven-plugin创建一个测试数据库,结合 maven surefire 插件参数和并行执行,似乎是一个很好的开始方法。
我不明白的是如何使用surefire插件设置的系统属性,以及sql-maven-plugin(或任何其他插件)。我有一个失败的设置。
万无一失的插件配置:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<forkCount>2</forkCount>
<reuseForks>true</reuseForks>
<argLine>-Xmx1024m -XX:MaxPermSize=256m</argLine>
<systemPropertyVariables>
<databaseSchema>test_schema_${surefire.forkNumber}</databaseSchema>
</systemPropertyVariables>
</configuration>
</plugin>
sql-maven-plugin 配置:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sql-maven-plugin</artifactId>
<version>1.5</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector-java.version}</version>
</dependency>
</dependencies>
<configuration>
<driver>com.mysql.jdbc.Driver</driver>
<username>user_with_create_privs</username>
<password>password</password>
<url>jdbc:mysql://localhost/dummy_schema</url>
</configuration>
<executions>
<execution>
<id>create-db</id>
<phase>process-test-classes</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<url>jdbc:mysql://localhost/dummy_schema</url>
<autocommit>true</autocommit>
<sqlCommand>create database ${databaseSchema}</sqlCommand>
</configuration>
</execution>
</executions>
</plugin>
外壳执行:
mvn test
结果输出:
[DEBUG] SQL: drop database ${databaseSchema}
[ERROR] Failed to execute: drop database ${databaseSchema}
[ERROR] Failed to execute goal org.codehaus.mojo:sql-maven-plugin:1.5:execute
(create-db) on project billing-core: You have an error in your SQL syntax; check
the manual that corresponds to your MySQL server version for the right
syntax to use near '{databaseSchema}' at line 1 -> [Help 1]
所以,看起来 ${databaseSchema} 没有填充,或者语法问题。我已经摆弄了,但是尽管看起来很容易并且应该这样做,但无法使其正常工作。