播放 2 框架单元测试 - java.lang.RuntimeException: DataSource 用户为空?
Posted
技术标签:
【中文标题】播放 2 框架单元测试 - java.lang.RuntimeException: DataSource 用户为空?【英文标题】:Play 2 framework unit tests - java.lang.RuntimeException: DataSource user is null? 【发布时间】:2013-02-11 21:31:32 【问题描述】:我尝试在 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
方法中抛出异常。
我做错了什么?
附:我没有使用play.test.Helpers.running
,因为它以Runnable
作为参数,而runnable 中的run()
不能抛出异常。因此,要在单元测试中捕获异常,我必须将整个 run()
包装到 try
中,捕获异常,单元测试失败并手动记录消息。它使代码膨胀很多。这是running()
的代码:
public static synchronized void running(FakeApplication fakeApplication, final Runnable block)
try
start(fakeApplication);
block.run();
finally
stop(fakeApplication);
我在我的@Before
和@After
中也这样做了,也许除了一些可能导致错误的线程。
【问题讨论】:
【参考方案1】:问题是由于配置中的ebean.default
键在主配置中被注释掉了:
ebean.default="models.*"
并且为 H2 设置用户和密码是不需要,没有它也可以:
settings.put("db.default.user", "sa");
settings.put("db.default.password", "");
【讨论】:
以上是关于播放 2 框架单元测试 - java.lang.RuntimeException: DataSource 用户为空?的主要内容,如果未能解决你的问题,请参考以下文章