我试图在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
方法中抛出。
我做错了什么?
P.S。我没有使用play.test.Helpers.running
,因为它需要Runnable
作为参数,而runnable中的run()
不能抛出异常。因此,要在单元测试中捕获异常,我必须将整个run()
包装到try
中,捕获异常,手动失败unittest并记录消息。它膨胀了很多代码。以下是running()
的代码:
public static synchronized void running(FakeApplication fakeApplication, final Runnable block) {
try {
start(fakeApplication);
block.run();
} finally {
stop(fakeApplication);
}
}
我在@Before
和@After
中也这样做,可能除了一些可能导致错误的线程之外。
答案 0 :(得分:14)
问题是由配置中的ebean.default
键在主配置中注释掉的:
ebean.default="models.*"
设置H2的用户和密码是而不是必要,它没有它的工作:
settings.put("db.default.user", "sa");
settings.put("db.default.password", "");