我可以使用HSQLDB进行junit测试克隆mySQL数据库

我正在开发一个spring webflow项目,我想我可以使用HSQLDB而不是我的mysql进行junit测试吗?

如何将我的mysql数据库克隆到HSQLDB中

如果您使用的是弹簧3.1或更高版本,则可以使用弹簧轮廓来实现此目的。 未设置活动配置文件时,将加载默认配置文件。

   ...other datasource properties also create or drop db      ...other datasource properties   

在unit testing中,通过添加注释来设置活动配置文件。

 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:app-config.xml") @ActiveProfiles("dev") public class TransferServiceTest { 

除了这些建议之外,考虑到根据您的需求,HSQL和MySQL没有相同的function,如合并连接和其他SQL非标准function。 因此(在我们的例子中),我们总是在嵌入式Mysql上运行测试。

嵌入式MySQL随mysql-connector-mxj一起提供。 如果你使用Maven,你可以这样得到它:

  mysql mysql-connector-mxj 5.0.12   mysql mysql-connector-mxj-db-files 5.0.12  

一旦驱动程序位于项目的路径上,您就可以从Java启动数据库。 在我们的例子中,我们有这个启动数据库的实用程序类:

 import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; import org.apache.commons.io.FileUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.mysql.management.MysqldResource; import com.mysql.management.MysqldResourceI; public class EmbeddedMySQLDb { protected Logger logger = LoggerFactory.getLogger(this.getClass()); private MysqldResource mysqldResource; private String baseDatabaseDir = System.getProperty("java.io.tmpdir"); private String databaseName = "test_db_" + System.nanoTime(); private int port = 13306; private String username = "root"; private String password = "password"; /** * Starts the mysql database * @return */ public void startDatabase() { if (logger.isDebugEnabled()) { logger.debug("=============== Starting Embedded MySQL using these parameters ==============="); logger.debug("baseDatabaseDir : " + baseDatabaseDir); logger.debug("databaseName : " + databaseName); logger.debug("host : localhost (hardcoded)"); logger.debug("port : " + port); logger.debug("username : " + username); logger.debug("password : " + password); logger.debug("============================================================================="); } File databaseDir = new File(new File(baseDatabaseDir), databaseName); mysqldResource = new MysqldResource(databaseDir); Map database_options = new HashMap(); database_options.put(MysqldResourceI.PORT, Integer.toString(port)); database_options.put(MysqldResourceI.INITIALIZE_USER, "true"); database_options.put(MysqldResourceI.INITIALIZE_USER_NAME, username); database_options.put(MysqldResourceI.INITIALIZE_PASSWORD, password); mysqldResource.start("embedded-mysqld-thread-" + System.currentTimeMillis(), database_options); if (!mysqldResource.isRunning()) { throw new RuntimeException("MySQL did not start."); } logger.info("MySQL started successfully @ " + System.currentTimeMillis()); } /** * Shutdowns the mysql database * @return */ public void shutdownDatabase() { mysqldResource.shutdown(); if (mysqldResource.isRunning() == false) { logger.info(">>>>>>>>>> DELETING MYSQL BASE DIR [" + mysqldResource.getBaseDir() + "] <<<<<<<<<<"); try { FileUtils.forceDelete(mysqldResource.getBaseDir()); } catch (IOException e) { logger.error(e.getMessage(), e); } } } //------------------------------------------------------------------------- /** * @return the baseDatabaseDir */ public final String getBaseDatabaseDir() { return baseDatabaseDir; } /** * @param baseDatabaseDir the baseDatabaseDir to set */ public final void setBaseDatabaseDir(String baseDatabaseDir) { this.baseDatabaseDir = baseDatabaseDir; } /** * @return the databaseName */ public final String getDatabaseName() { return databaseName; } /** * @param databaseName the databaseName to set */ public final void setDatabaseName(String databaseName) { this.databaseName = databaseName; } /** * @return the port */ public final int getPort() { return port; } /** * @param port the port to set */ public final void setPort(int port) { this.port = port; } /** * @return the username */ public final String getUsername() { return username; } /** * @param username the username to set */ public final void setUsername(String username) { this.username = username; } /** * @return the password */ public final String getPassword() { return password; } /** * @param password the password to set */ public final void setPassword(String password) { this.password = password; } } 

要创建一次连接到DB,您只需将DB url用作以下内容:

String url = "jdbc:mysql:mxj://localhost:" + port + "/" + dbName;

问候,