我想知道是否有人使用 Hibernate 作为持久性引擎使用 Play 2.0 进行自动化测试?
我正在尝试以下测试,但出现此错误:
[error] Test tests.TestApp.findById failed: No EntityManager bound to this threa
d. Try to annotate your action method with @play.db.jpa.Transactional
[error] at play.db.jpa.JPA.em(JPA.java:45)
[error] at models.User.findById(User.java:72)
[error] at tests.TestApp$1.run(TestApp.java:55)
[error] at play.test.Helpers.running(Helpers.java:277)
[error] at tests.TestApp.findById(TestApp.java:51)
测试代码为:
@Test
public void findById()
{
running(fakeApplication(), new Runnable()
{
public void run()
{
User user = User.findById(21l);
assertThat(user.getName()).isEqualTo("Dave");
}
});
}
我尝试将 @Transactional 注释添加到测试方法中,但这没有效果。
然后我尝试添加绑定到当前 JPA 线程的 setup 和 teardown 方法,但也没有运气。虽然我不确定我是否做得正确。
private EntityManager em;
@Transactional
@Before
public void setUp()
{
em = JPA.em("org.hibernate.ejb.HibernatePersistence");
JPA.bindForCurrentThread(em);
}
@After
public void tearDown()
{
JPA.bindForCurrentThread(null);
em.close();
}
这给出了以下错误:
[error] Test tests.TestApp.findById failed: There is no started application
[error] at scala.sys.package$.error(package.scala:27)
[error] at play.api.Play$$anonfun$current$1.apply(Play.scala:44)
[error] at play.api.Play$$anonfun$current$1.apply(Play.scala:44)
[error] at scala.Option.getOrElse(Option.scala:108)
[error] at play.api.Play$.current(Play.scala:44)
[error] at play.api.Play.current(Play.scala)
[error] at play.Play.application(Play.java:12)
[error] at play.db.jpa.JPA.em(JPA.java:21)
[error] at tests.TestApp.setUp(TestApp.java:26)
[error] ...
如果有人可以提供任何帮助,我将不胜感激!