Testing 试图运行JUnit测试,但出现错误:实现类

Testing 试图运行JUnit测试,但出现错误:实现类,testing,junit,persistence,entity,Testing,Junit,Persistence,Entity,我正在尝试使用以下类(DataStructureTest)代码运行一个简单的测试: } DatabaseTestCase类代码如下: public class DatabaseTestCase { /** * This method establishes Entity Managerin before every test * and writes information into log. */ private static final Log log = LogFactory.getLo

我正在尝试使用以下类(DataStructureTest)代码运行一个简单的测试:

}

DatabaseTestCase类代码如下:

public class DatabaseTestCase {

/**
* This method establishes Entity Managerin before every test
* and writes information into log.
*/
private static final Log log = LogFactory.getLog(DatabaseTestCase.class);
private EntityManager entityManager;
private EntityManagerFactory entityManagerFactory;

@Before
public void establishEntityManager(){
    log.debug("Establishing database connection!");
    entityManagerFactory = Persistence.createEntityManagerFactory("warehouseTestPersistence", null);
    entityManager = entityManagerFactory.createEntityManager();
}

@After
public void closeEntityManager(){
   if(entityManager != null){
        entityManager.close();           
   } else {
       log.warn("Entity was empty (null) in tests.");
   }
}

public EntityManager getEntityManager(){
    return entityManager;
}

/**
 * @param entityManager the entityManager to set
 */
public void setEntityManager(final EntityManager entityManager) {
    this.entityManager = entityManager;
}

/**
 * @return the entityManagerFactory
 */
public EntityManagerFactory getEntityManagerFactory() {
    return entityManagerFactory;
}

/**
 * @param entityManagerFactory the entityManagerFactory to set
 */
public void setEntityManagerFactory(final EntityManagerFactory entityManagerFactory) {
    this.entityManagerFactory = entityManagerFactory;
}

/**
* Save entity to database. Used for assisting in tests.
* @return primary key for specific entity.
*/

protected Integer saveEntity(Entityclass entity){
    EntityManager em = getEntityManager();
    EntityTransaction tx = em.getTransaction();
    tx.begin();
    em.persist(entity);
    tx.commit();
    return entity.getId();
}

/**
 * Load entity from save location.
 * @param <T>
 *      Generic class.
 * @param entityclass
 *      Entity class of entity that is going to be loaded.
 * @param primarykey
 *      Entity class primary key.
 * @return correspondant entity.
 */
protected <T extends Entityclass> T loadEntity(final Class<T> entitysclass, final Integer primarykey){
    EntityManager em = getEntityManager();
    EntityTransaction tx = em.getTransaction();
    tx.begin();
    T entity = em.find(entitysclass, primarykey);
    tx.commit();
    return entity;
}
公共类数据库testcase{
/**
*此方法在每次测试之前建立实体管理器
*并将信息写入日志。
*/
私有静态最终日志Log=LogFactory.getLog(DatabaseTestCase.class);
私人实体管理者实体管理者;
私人实体管理工厂实体管理工厂;
@以前
公共无效机构管理器(){
调试(“建立数据库连接!”);
entityManagerFactory=Persistence.createEntityManagerFactory(“warehouseTestPersistence”,null);
entityManager=entityManagerFactory.createEntityManager();
}
@之后
public void closeEntityManager(){
如果(entityManager!=null){
entityManager.close();
}否则{
warn(“实体在测试中为空(null)”;
}
}
公共实体管理器getEntityManager(){
返回实体管理器;
}
/**
*@param entityManager要设置的entityManager
*/
公共无效设置实体管理器(最终实体管理器实体管理器){
this.entityManager=entityManager;
}
/**
*@返回EntityManager工厂
*/
公共EntityManagerFactory getEntityManagerFactory(){
返回实体管理工厂;
}
/**
*@param entityManagerFactory要设置的entityManagerFactory
*/
公共无效设置EntityManagerFactory(最终EntityManagerFactory EntityManagerFactory){
this.entityManagerFactory=entityManagerFactory;
}
/**
*将实体保存到数据库。用于协助测试。
*@返回特定实体的主键。
*/
受保护的整数存储实体(Entityclass实体){
EntityManager em=getEntityManager();
EntityTransaction tx=em.getTransaction();
tx.begin();
em.persist(实体);
tx.commit();
返回entity.getId();
}
/**
*从保存位置加载实体。
*@param
*泛型类。
*@param entityclass
*要加载的实体的实体类。
*@param primarykey
*实体类主键。
*@返回对应实体。
*/
受保护的T loadEntity(最终类EntityClass,最终整数primarykey){
EntityManager em=getEntityManager();
EntityTransaction tx=em.getTransaction();
tx.begin();
T entity=em.find(entitysclass,primarykey);
tx.commit();
返回实体;
}
}

我使用NetBeans 7.0.1。两个类的所有导入都已正确完成。我还添加了所有必需的依赖项。但每当我运行测试时,我都会得到以下结果:

--误差域--

未通过测试,1个测试导致错误。(0075 s) com.mysite.warehouseapp.DataStructureTest失败 testProductData导致错误:实现类

实现类 java.lang.CompatibleClassChangeError 在 在com.mysite.warehouseapp.test.DatabaseTestCase.EstablishtyManager(DatabaseTestCase.java:36)上

--测试结果字段--

  • 建立数据库连接
  • 实体在测试中为空(null)
如果你看到顶部的测试结果图像,我不知道它是如何到达那里的,但它应该在这个位置

简单地说,每当我尝试运行测试时,都没有实体。有人能告诉我原因吗?我四天来一直试图找到解决这个问题的办法,但还是没有成功。感谢您的帮助


谢谢

我自己找到了解决方案,这是一个非常有益的解决方案:)。无论如何,这个问题的解决方案是persistence.xml位于错误的文件夹中。对于新手,我建议您将persistence.xml放在项目src/main/resources/META-INF/persistence.xml的以下文件夹下

如果您没有在src/test下创建resources/META-INF文件夹,那么创建一个并将persistence.xml文件放在那里。我希望这对某人有所帮助:)


我发现的另一件事是,您可能有不同的罐子,它们彼此“对立”,因此您需要取出这些罐子,然后它就会工作。

有什么帮助吗?
public class DatabaseTestCase {

/**
* This method establishes Entity Managerin before every test
* and writes information into log.
*/
private static final Log log = LogFactory.getLog(DatabaseTestCase.class);
private EntityManager entityManager;
private EntityManagerFactory entityManagerFactory;

@Before
public void establishEntityManager(){
    log.debug("Establishing database connection!");
    entityManagerFactory = Persistence.createEntityManagerFactory("warehouseTestPersistence", null);
    entityManager = entityManagerFactory.createEntityManager();
}

@After
public void closeEntityManager(){
   if(entityManager != null){
        entityManager.close();           
   } else {
       log.warn("Entity was empty (null) in tests.");
   }
}

public EntityManager getEntityManager(){
    return entityManager;
}

/**
 * @param entityManager the entityManager to set
 */
public void setEntityManager(final EntityManager entityManager) {
    this.entityManager = entityManager;
}

/**
 * @return the entityManagerFactory
 */
public EntityManagerFactory getEntityManagerFactory() {
    return entityManagerFactory;
}

/**
 * @param entityManagerFactory the entityManagerFactory to set
 */
public void setEntityManagerFactory(final EntityManagerFactory entityManagerFactory) {
    this.entityManagerFactory = entityManagerFactory;
}

/**
* Save entity to database. Used for assisting in tests.
* @return primary key for specific entity.
*/

protected Integer saveEntity(Entityclass entity){
    EntityManager em = getEntityManager();
    EntityTransaction tx = em.getTransaction();
    tx.begin();
    em.persist(entity);
    tx.commit();
    return entity.getId();
}

/**
 * Load entity from save location.
 * @param <T>
 *      Generic class.
 * @param entityclass
 *      Entity class of entity that is going to be loaded.
 * @param primarykey
 *      Entity class primary key.
 * @return correspondant entity.
 */
protected <T extends Entityclass> T loadEntity(final Class<T> entitysclass, final Integer primarykey){
    EntityManager em = getEntityManager();
    EntityTransaction tx = em.getTransaction();
    tx.begin();
    T entity = em.find(entitysclass, primarykey);
    tx.commit();
    return entity;
}