尝试运行JUnit测试但得到错误:实现类

时间:2013-01-25 10:11:36

标签: testing junit persistence entity

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

public class DataStructureTest extends DatabaseTestCase {

private static final Log log = (Log) LogFactory.getLog(DataStructureTest.class);

@Test
public void testProductData(){
    //Creating a product data
    log.debug("Creating new product data");
    ProductData productData = new ProductData("Öljy");

    //Setting basic information of product
    log.debug("Setting basic information like name, price, description "
            + "and amount.");
    productData.setProductPrice(4.13);
    productData.setProductDescription("Öljy rekoille.");
    productData.setProductAmount(20);

    //Saving entity
    EntityManager em = getEntityManager();
    EntityTransaction tx = em.getTransaction();
    tx.begin();
    em.persist(productData);
    Integer primarykey = productData.getId();
    tx.commit();

    //Reading entity from database
    log.debug("Reading entity data from database.");
    tx = em.getTransaction();
    tx.begin();
    productData = em.find(ProductData.class, primarykey);
    tx.commit();   

    //Asserting that the product data was correct in database
    Assert.assertEquals("Product name was correct", "Öljy", 
            productData.getProductName());
    Assert.assertEquals("Product price was correct", 4.13, 
            productData.getProductPrice());
    Assert.assertEquals("Product description was correct", "Öljy rekoille", 
            productData.getProductDescription());
    Assert.assertEquals("Product amount was incorrect", 21, 
            productData.getProductAmount());

}

}

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;
}

}

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

- 错误字段 -

没有测试通过,1次测试导致错误。(0,075 s) com.mysite.warehouseapp.DataStructureTest失败 testProductData导致错误:实现类

实施课程 java.lang.IncompatibleClassChangeError 在.... 在com.mysite.warehouseapp.test.DatabaseTestCase.establishEntityManager(DatabaseTestCase.java:36)

- 测试结果字段 -

  • 建立数据库连接!
  • 实验中的实体为空(null)。

如果您在顶部看到测试结果图像,我不知道它是如何到达那里但它应该在这个位置。

所以简单地说,每当我尝试运行测试时,都没有实体。谁能告诉我原因?我试图找到这个问题的解决方案四天但仍然没有运气。任何帮助表示赞赏。

由于

1 个答案:

答案 0 :(得分:0)

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

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

我发现的另一件事是你可能有不同的罐子相互“反对”,因此你需要取出那些罐子然后它才能起作用。