简体   繁体   English

如何声明第二个数据库以在Play Framework中进行测试?

[英]How to declare second database for testing in Play Framework?

I want to run unit tests on a database other than the default one. 我想在默认数据库以外的数据库上运行单元测试。 Here is my application.conf : 这是我的application.conf

application.secret="[cut]"
application.langs="en"

db.default.driver=com.mysql.jdbc.Driver
db.default.url="jdbc:mysql://localhost:3306/city_game?characterEncoding=UTF-8"
db.default.user=root
db.default.password=""

db.test.driver=com.mysql.jdbc.Driver
db.test.url="jdbc:mysql://localhost:3306/play_test?characterEncoding=UTF-8"
db.test.user=root
db.test.password=""

ebean.default="models.*"
ebean.test="models.*"

logger.root=ERROR
logger.play=INFO
logger.application=DEBUG

BaseModelTest.java: BaseModelTest.java:

package models;

import com.avaje.ebean.Ebean;
import com.avaje.ebean.EbeanServer;
import com.avaje.ebean.config.ServerConfig;
import com.avaje.ebeaninternal.server.ddl.DdlGenerator;
import com.avaje.ebean.config.dbplatform.MySqlPlatform;
import com.avaje.ebeaninternal.api.SpiEbeanServer;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import play.test.FakeApplication;
import play.test.Helpers;

import java.io.IOException;

public class BaseModelTest
{
    public static FakeApplication app;
    public static DdlGenerator ddl;

    @BeforeClass
    public static void startApp() throws IOException
    {
        app = Helpers.fakeApplication();
        Helpers.start(app);

        String serverName = "test";
        EbeanServer server = Ebean.getServer(serverName);
        ServerConfig config = new ServerConfig();
        ddl = new DdlGenerator();
        ddl.setup((SpiEbeanServer) server, new MySqlPlatform(), config);
    }

    @AfterClass
    public static void stopApp()
    {
        Helpers.stop(app);
    }

    @Before
    public void dropCreateDb() throws IOException
    {

        // Drop
        ddl.runScript(false, ddl.generateDropDdl());
        // Create
        ddl.runScript(false, ddl.generateCreateDdl());
    }
}

However, I get results saved in the default database, and the test one has its tables created but empty. 但是,我将结果保存在默认数据库中,并且测试一个表已创建但为空。 What I expect is to have the results written to the test db and default one untouched. 我期望将结果写入测试数据库,并且默认情况下保持不变。

I somehow ended with different approach. 我以某种方式结束了。 I still created separate real test database instance (because of stored procedures), but instead I used the Play1-like approach. 我仍然创建了单独的真实测试数据库实例(由于存储过程),但是我使用了类似于Play1的方法。

I have separates configuration sides beneath my main configuration (eg test configuration, prod specific stuff, stage specific stuff etc ..) 我在主要配置下有单独的配置侧(例如,测试配置,特定于产品的产品,特定于阶段的产品等..)

I load it via Global.scala as shown below (please note the exaple provided below works in in Play for java developers version as well) 我通过Global.scala加载它,如下所示(请注意,下面提供的示例在Play中也适用于Java开发人员版本)

object Global extends GlobalSettings {
  override def onLoadConfig(config: Configuration, path: File, cl: ClassLoader, mode: Mode.Mode): Configuration = {
     val modeFile: String = s"application.${mode.toString.toLowerCase}.conf"

     Logger.error(s"Loading {${path.toURI}conf/application.conf}")
     Logger.error(s"Appending mode specific configuration {${path.toURI}conf/$modeFile}")
     val modeConfig = config ++ Configuration(ConfigFactory.load(modeFile))

     super.onLoadConfig(modeConfig, path, cl, mode)
   }
}

And the application.test.conf config file is as follows: 并且application.test.conf配置文件如下:

# test database
db.default.logStatements=false
db.default.jndiName=DefaultDS
db.default.url="jdbc:postgresql://127.0.0.1:5432/db-test"
db.default.user=user
db.default.password="password!@#$"
db.default.driver=org.postgresql.Driver

This way I get following benefits: 这样,我可以获得以下好处:

  • I still write my tests the usual way 我仍然以通常的方式编写测试
  • Play evolutions gets tested on CI / jenkins as well 游戏进化也在CI / jenkins上得到测试
  • I have to write my tests in the way I could safely retun them on the existing db instance w/ minimal assumptions about data and userbase. 必须以可以在现有数据库实例上安全地重新调整它们的方式编写测试,而无需对数据和用户群进行最小假设。 That way I'm 90% certain I will be able to run those against staging / prod environment with much less friction. 这样,我可以90%地确定我将能够以较小的摩擦在暂存/生产环境中运行这些程序。 (Controversial point) (争议点)

I think you should separate your code 我认为您应该分开代码

as these 像这些

 @BeforeClass
    public static void startApp() throws IOException {

        app = Helpers.fakeApplication();
        Helpers.start(app);
    }

 @Before
    public void dropCreateDb() throws IOException {

        String serverName = "test";

        EbeanServer server = Ebean.getServer(serverName);

        ServerConfig config = new ServerConfig();

        DdlGenerator ddl = new DdlGenerator((SpiEbeanServer) server, new MySqlPlatform(), config);

        // Drop
        ddl.runScript(false, ddl.generateDropDdl());

        // Create
        ddl.runScript(false, ddl.generateCreateDdl());
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM