2

我尝试在 MySQL 兼容模式下使用 H2 数据库在 Play 2 应用程序中运行单元测试。我将其配置@Before如下:

@Before
public void startApp() throws Exception {
    Map<String, String> settings = new HashMap<String, String>();
    settings.put("db.default.driver", "org.h2.Driver");
    settings.put("db.default.user", "sa");
    settings.put("db.default.password", "");
    settings.put("db.default.url", "jdbc:h2:mem:play-test-351881363;MODE=MySQL"); // TODO: use config for url
    settings.put("db.default.jndiName", "DefaultDS");
    app = Helpers.fakeApplication(settings);
    Helpers.start(app);

    databaseTester = new JndiDatabaseTester("DefaultDS");

    IDataSet expectedDataSet = new FlatXmlDataSetBuilder().build(play.Play.application()
            .resourceAsStream("/resources/dataset.xml"));
    databaseTester.setDataSet(expectedDataSet);
    databaseTester.onSetup();
}

@After
public void stopApp() throws Exception {
    databaseTester.onTearDown();
    Helpers.stop(app);
}

但是java.lang.RuntimeException: DataSource user is null?当它尝试访问数据库时会导致实际的测试方法:

[ERROR] [02/12/2013 01:19:32.085] [application-akka.actor.default-dispatcher-4] [akka://application/user/load_data_task_runner_test/load_record] DataSource user is null?
java.lang.RuntimeException: DataSource user is null?
    at com.avaje.ebeaninternal.server.lib.sql.DataSourcePool.<init>(DataSourcePool.java:184)
    at com.avaje.ebeaninternal.server.lib.sql.DataSourceManager.getDataSource(DataSourceManager.java:200)
    at com.avaje.ebeaninternal.server.lib.sql.DataSourceGlobalManager.getDataSource(DataSourceGlobalManager.java:46)
    at com.avaje.ebeaninternal.server.core.DefaultServerFactory.getDataSourceFromConfig(DefaultServerFactory.java:432)
    at com.avaje.ebeaninternal.server.core.DefaultServerFactory.setDataSource(DefaultServerFactory.java:393)
    at com.avaje.ebeaninternal.server.core.DefaultServerFactory.createServer(DefaultServerFactory.java:169)
    at com.avaje.ebeaninternal.server.core.DefaultServerFactory.createServer(DefaultServerFactory.java:124)
    at com.avaje.ebeaninternal.server.core.DefaultServerFactory.createServer(DefaultServerFactory.java:64)
    at com.avaje.ebean.EbeanServerFactory.create(EbeanServerFactory.java:45)
    at com.avaje.ebean.Ebean$ServerManager.getWithCreate(Ebean.java:206)
    at com.avaje.ebean.Ebean$ServerManager.get(Ebean.java:193)
    at com.avaje.ebean.Ebean$ServerManager.access$200(Ebean.java:128)
    at com.avaje.ebean.Ebean.getServer(Ebean.java:257)
    at play.db.ebean.Model$Finder.server(Model.java:240)
    at play.db.ebean.Model$Finder.byId(Model.java:261)
    at integration.LoadRecordService.onReceive(LoadRecordService.java:50)
    at akka.actor.UntypedActor$$anonfun$receive$1.applyOrElse(UntypedActor.scala:159)
    at akka.actor.ActorCell.receiveMessage(ActorCell.scala:425)
    at akka.actor.ActorCell.invoke(ActorCell.scala:386)
    at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:230)
    at akka.dispatch.Mailbox.run(Mailbox.scala:212)
    at akka.dispatch.ForkJoinExecutorConfigurator$MailboxExecutionTask.exec(AbstractDispatcher.scala:502)
    at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:262)
    at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:975)
    at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1478)
    at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:104)

这里使用dbunit进行演化和初始化startApp(),在方法中抛出异常@Test

我做错了什么?

PS我没有使用play.test.Helpers.running,因为它Runnable作为参数并且run()在runnable中不能抛出异常。因此,要在单元测试中捕获异常,我必须将整个包装run()放入try,捕获异常,单元测试失败并手动记录消息。它使代码膨胀很多。这是代码running()

public static synchronized void running(FakeApplication fakeApplication, final Runnable block) {
    try {
        start(fakeApplication);
        block.run();
    } finally {
        stop(fakeApplication);
    }
}

我在我的@Beforeand中做同样的事情@After,也许除了一些可能导致错误的线程内容。

4

1 回答 1

14

问题是由配置中的ebean.default键引起的,在主配置中被注释掉:

ebean.default="models.*"

并且不需要为 H2 设置用户名和密码,没有它也可以:

settings.put("db.default.user", "sa");
settings.put("db.default.password", "");
于 2013-02-12T08:47:07.473 回答