0

我正在尝试使用以下类(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 秒)com.mysite.warehouseapp.DataStructureTest FAILED testProductData 导致错误:实现类

在 com.mysite.warehouseapp.test.DatabaseTestCase.establishEntityManager(DatabaseTestCase.java:36) 处实现类 java.lang.IncompatibleClassChangeError

-- 测试结果字段 --

  • 建立数据库连接!
  • 实体在测试中为空(null)。

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

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

谢谢

4

1 回答 1

0

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

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

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

于 2013-02-07T14:05:07.937 回答