无法获取数据源尝试单元测试java持久性实体管理器

时间:2016-03-27 18:58:28

标签: java jpa eclipselink datasource

我是JPA的新手,我想测试我的EntityManagers持久性/删除等。我一直收到一个我无法收到的错误 数据源 。我完全输了,我在网上搜索了几个小时,无法在网上找到(或至少理解)任何答案。任何帮助,将不胜感激。谢谢!

这是测试类。

@Transactional
public class DoctorRepositoryTest
{
   private DoctorRepository dr;
   private Doctor doctor;
   private EntityManagerFactory emf
       = Persistence.createEntityManagerFactory("TestPersistence");
  private  EntityManager em = null;


public DoctorRepositoryTest()
{
}

@BeforeClass
public static void setUpClass()
{

}

@AfterClass
public static void tearDownClass()
{
}

@Before
public void setUp()
{




  dr = new DoctorRepository();

  doctor = new Doctor(0,"doctorFirstName", "doctorLastName", "4032223333", 
  "123 45 ave NW","Calgary", "AB", "t3k5c9");


}

@After
public void tearDown()
{
   dr = null;
   doctor = null;
}

/**
 * Testings method for the insertion of Doctor Objects to
 * the database
 */
@Test
public void testInsert(){



    int result;
    EntityManager em = emf.createEntityManager();
     try
   {
      em.getTransaction().begin();
      em.persist(doctor);
      em.getTransaction().commit();
      result = 1;
   }
     catch(Exception e){

        result = 0;
     }
   finally
   {
      em.close();
   }

     assertEquals(result,1);

}

这是我的持久性xml

    <persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence"            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence   http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="SunrisePU" transaction-type="JTA">
   <jta-data-source>SunriseDB</jta-data-source>
   <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties/>
  </persistence-unit>
  <persistence-unit name="TestPersistence" transaction-type="RESOURCE_LOCAL">
    <jta-data-source>SunriseDB</jta-data-source>
    <properties>
    </properties>
  </persistence-unit>
</persistence>

我一直遇到的错误是。

[EL Info]: 2016-03-27 12:50:44.151--ServerSession(957465255)--EclipseLink, version: Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd

[EL Severe]: ejb: 2016-03-27 12:50:48.588--ServerSession(957465255)--Exception     
[EclipseLink-7060] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.ValidationException

异常说明:无法获取数据源[SunriseDB]。

错误还有很多,但根本原因是我无法连接到我的数据库进行测试。

1 个答案:

答案 0 :(得分:0)

问题可能是你在persistence.xml文件的属性部分没有任何内容......

    <properties>
        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://127.0.0.1:3306/prytest"/>
        <property name="javax.persistence.jdbc.user" value="root"/>
        <property name="javax.persistence.jdbc.password" value="root"/>
        <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
    </properties>

根据您的DBMS(在我的情况下是MySql)

检查驱动程序的正确配置