gpt4 book ai didi

sql - 使用 Maven 对 Postgres 数据库进行单元测试

转载 作者:行者123 更新时间:2023-12-02 03:46:32 24 4
gpt4 key购买 nike

首先让我解释一下我在做什么:

在 Eclipse (JUNO) 中创建 Java 项目以对 Postgres 数据库的 SQL 进行单元测试。

使用 Maven2 作为构建工具。我有 DBUnit 和 SQL maven 插件。

目标是删除模式并重建表并在表中加载一些数据。

我已经测试了 SQL,所以我知道它可以工作。我已经测试了连接,所以我知道 URL 是正确的。

现在谈谈我的问题。我是 Maven 的新手,用于单元测试 SQL。我已尝试遵循大部分在线文档。我已经根据示例创建了我的 pom。当 Java 代码编译时,pom.xml 文件中的 SQL 工作导致构建失败。这是我的 pom.xml 文件:

<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>

<groupId>com.premierinc.esd</groupId>
<artifactId>sqlunittest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>sqlunittest</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.version>3.8.1</junit.version>
<maven.compiler.plugin.version>2.5.1</maven.compiler.plugin.version>
<sql.maven.plugin.version>1.5</sql.maven.plugin.version>
<postgresql.jdbc.version>9.1-901.jdbc4</postgresql.jdbc.version>
</properties>

<repositories>
<repository>
<id>central</id>
<name>Central Repository</name>
<url>http://repo.maven.apache.org/maven2</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>

<pluginRepositories>
<pluginRepository>
<id>central</id>
<name>Central Repository</name>
<url>http://repo.maven.apache.org/maven2</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<updatePolicy>never</updatePolicy>
</releases>
</pluginRepository>
</pluginRepositories>


<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgresql.jdbc.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<pluginManagement>

<plugins>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin.version}</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sql-maven-plugin</artifactId>
<version>${sql.maven.plugin.version}</version>

<dependencies>
<!-- specify the dependent jdbc driver here -->
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgresql.jdbc.version}</version>
</dependency>
</dependencies>

<!-- common configuration shared by all executions -->
<configuration>
<driver>org.postgresql.Driver</driver>
<url>jdbc:postgresql://localhost:5432:testdb</url>
<username>postgres</username>
<password>root</password>
<!-- You can comment out username/password configurations and have
maven to look them up in your settings.xml using ${settingsKey} -->
<settingsKey>sensibleKey</settingsKey>
<!--all executions are ignored if -Dmaven.test.skip=true -->
<skip>${maven.test.skip}</skip>
</configuration>


<executions>
<execution>
<id>drop-schema-before-test-if-any</id>
<phase>process-test-resources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<!-- need another database to drop the targeted one -->
<url>jdbc:postgresql://localhost:5432:postgres</url>
<autocommit>true</autocommit>
<sqlCommand>DROP SCHEMA chipen CASCADE</sqlCommand>
<!-- ignore error when database is not available -->
<onError>continue</onError>
</configuration>
</execution>


<execution>
<id>create-schema</id>
<phase>process-test-resources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<autocommit>true</autocommit>
<srcFiles>
<srcFile>src/main/sql/CHI-PEN-schema.sql</srcFile>
</srcFiles>
</configuration>
</execution>

</executions>
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>dbunit-maven-plugin</artifactId>
<version>1.0-beta-3</version>


<dependencies>
<!-- specify the dependent jdbc driver here -->
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgresql.jdbc.version}</version>
</dependency>
</dependencies>

<!-- common configuration shared by all executions -->
<configuration>
<driver>org.postgresql.Driver</driver>
<url>jdbc:postgresql://localhost:5432:testdb</url>
<username>postgres</username>
<password>root</password>
<!-- You can comment out username/password configurations and have
maven to look them up in your settings.xml using ${settingsKey} -->
<settingsKey>sensibleKey</settingsKey>
<!--all executions are ignored if -Dmaven.test.skip=true -->
<skip>${maven.test.skip}</skip>
</configuration>

<executions>

<execution>

<phase>test-compile</phase>
<goals>
<goal>operation</goal>
</goals>
<!-- specific configurations -->
<configuration>
<type>CLEAN_INSERT</type>
<src>src/test/data/testdb_chipen_data.xml</src>
</configuration>
</execution>
</executions>

</plugin>
</plugins>
</pluginManagement>
</build>

如有任何建议或提示,我们将不胜感激。

提前致谢。

最佳答案

我在此处以更正的形式添加您帖子的 pom。我特别删除了存储库定义,因为它们是 Maven 中的默认设置,因此配置约定意味着只定义真正需要定义的内容。此外,我已经删除了 pluginManagement 标签,因为这意味着不真正执行它意味着定义的东西。更准确的说pluginManagement旨在定义插件的版本,但通常不是配置。这通常在父 pom 中使用:

<project ...>
..
<build>
<pluginManagement>
<plugins>
<plugin>
Plugin groupId, artifactId, version
</plugin>
.
</plugin>
</pluginManagement>
..
</project>

让我们回到你的 pom.下面应该运行:

<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>

<groupId>com.premierinc.esd</groupId>
<artifactId>sqlunittest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>sqlunittest</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.version>3.8.1</junit.version>
<maven.compiler.plugin.version>2.5.1</maven.compiler.plugin.version>
<sql.maven.plugin.version>1.5</sql.maven.plugin.version>
<postgresql.jdbc.version>9.1-901.jdbc4</postgresql.jdbc.version>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgresql.jdbc.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin.version}</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sql-maven-plugin</artifactId>
<version>${sql.maven.plugin.version}</version>

<dependencies>
<!-- specify the dependent jdbc driver here -->
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgresql.jdbc.version}</version>
</dependency>
</dependencies>

<!-- common configuration shared by all executions -->
<configuration>
<driver>org.postgresql.Driver</driver>
<url>jdbc:postgresql://localhost:5432:testdb</url>
<username>postgres</username>
<password>root</password>
<!-- You can comment out username/password configurations and have
maven to look them up in your settings.xml using ${settingsKey} -->
<settingsKey>sensibleKey</settingsKey>
<!--all executions are ignored if -Dmaven.test.skip=true -->
<skip>${maven.test.skip}</skip>
</configuration>

<executions>
<execution>
<id>drop-schema-before-test-if-any</id>
<phase>process-test-resources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<!-- need another database to drop the targeted one -->
<url>jdbc:postgresql://localhost:5432:postgres</url>
<autocommit>true</autocommit>
<sqlCommand>DROP SCHEMA chipen CASCADE</sqlCommand>
<!-- ignore error when database is not available -->
<onError>continue</onError>
</configuration>
</execution>


<execution>
<id>create-schema</id>
<phase>process-test-resources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<autocommit>true</autocommit>
<srcFiles>
<srcFile>src/main/sql/CHI-PEN-schema.sql</srcFile>
</srcFiles>
</configuration>
</execution>

</executions>
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>dbunit-maven-plugin</artifactId>
<version>1.0-beta-3</version>


<dependencies>
<!-- specify the dependent jdbc driver here -->
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgresql.jdbc.version}</version>
</dependency>
</dependencies>

<!-- common configuration shared by all executions -->
<configuration>
<driver>org.postgresql.Driver</driver>
<url>jdbc:postgresql://localhost:5432:testdb</url>
<username>postgres</username>
<password>root</password>
<!-- You can comment out username/password configurations and have
maven to look them up in your settings.xml using ${settingsKey} -->
<settingsKey>sensibleKey</settingsKey>
<!--all executions are ignored if -Dmaven.test.skip=true -->
<skip>${maven.test.skip}</skip>
</configuration>

<executions>

<execution>

<phase>test-compile</phase>
<goals>
<goal>operation</goal>
</goals>
<!-- specific configurations -->
<configuration>
<type>CLEAN_INSERT</type>
<src>src/test/data/testdb_chipen_data.xml</src>
</configuration>
</execution>
</executions>

</plugin>
</plugins>
</build>

除上述之外,这样的事情是集成测试,不是单元测试,但这是一个不同的问题/讨论。

关于sql - 使用 Maven 对 Postgres 数据库进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16600037/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com