2012-12-29 31 views
3

这似乎是一个类似的问题(Test DAO in java using Junit4 and Hibernate),但他正在成功创建会话。我无法创建会话。如何在JUnit4中使用休眠测试DAO

请注意,我没有使用Spring。我只需要测试自己的DAO其创建使用HibernateUtil.java

我的样品测试代码(测试包)

public void testGetBean() { 
    System.out.println("getBean"); 
    int idaccount = 0; 
    Account expResult = null; 
    Account result = DAOAccount.getBean(idaccount); 
    assertEquals(expResult, result); 
    // TODO review the generated test code and remove the default call to fail. 
    //fail("The test case is a prototype."); 
} 

会议这里是DAOAccount的getBean()(源包)

public static Account getBean(int idaccount) { 
    Account ac = null; 
    Session sess = NewHibernateUtil.getSessionFactory().openSession(); 
    Transaction tx = sess.beginTransaction(); 
    Query q = sess.createQuery("from Account where idaccount=:id"); 
    q.setInteger("id", idaccount); 
    ac = (Account) q.uniqueResult(); 
    tx.commit(); 
    sess.close(); 
    return ac; 
} 

NewHibernateUtil.java(源包)

public class NewHibernateUtil { 
private static final SessionFactory sessionFactory; 

static { 
    try { 
     // Create the SessionFactory from standard (hibernate.cfg.xml) 
     // config file. 
//   sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); 
     sessionFactory = new Configuration().configure().buildSessionFactory(); 
    } catch (Throwable ex) { 
     // Log the exception. 
     System.err.println("Initial SessionFactory creation failed." + ex); 
     throw new ExceptionInInitializerError(ex); 
    } 
} 

public static SessionFactory getSessionFactory() { 
    return sessionFactory; 
} 
} 

,这里是堆栈跟踪enter image description here

下面是项目结构:

enter image description here

实际的异常堆栈跟踪的要求由@Subin

[2012-12-29 16:36:24.347] [ INFO] Configuration.applyHibernateValidatorLegacyConstraintsOnDDL:1646 - Hibernate Validator not found: ignoring 
Initial SessionFactory creation failed.java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/validation/Validation 
java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/validation/Validation 
    at java.lang.ClassLoader.defineClass1(Native Method) 
    at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631) 
    at java.lang.ClassLoader.defineClass(ClassLoader.java:615) 
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141) 
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:283) 
    at java.net.URLClassLoader.access$000(URLClassLoader.java:58) 
    at java.net.URLClassLoader$1.run(URLClassLoader.java:197) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306) 
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247) 
    at java.lang.Class.forName0(Native Method) 
    at java.lang.Class.forName(Class.java:247) 
    at org.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:170) 
    at org.hibernate.cfg.beanvalidation.BeanValidationActivator.applyDDL(BeanValidationActivator.java:103) 
    at org.hibernate.cfg.Configuration.applyBeanValidationConstraintsOnDDL(Configuration.java:1674) 
    at org.hibernate.cfg.Configuration.applyConstraintsToDDL(Configuration.java:1624) 
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1415) 
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1826) 
    at dao.NewHibernateUtil.<clinit>(NewHibernateUtil.java:25) 
    at dao.DAOAccount.getAccounts(DAOAccount.java:77) 
    at dao.DAOAccountTest.testGetAccounts_User(DAOAccountTest.java:78) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
    at junit.framework.TestCase.runTest(TestCase.java:176) 
    at junit.framework.TestCase.runBare(TestCase.java:141) 
    at junit.framework.TestResult$1.protect(TestResult.java:122) 
    at junit.framework.TestResult.runProtected(TestResult.java:142) 
    at junit.framework.TestResult.run(TestResult.java:125) 
    at junit.framework.TestCase.run(TestCase.java:129) 
    at junit.framework.TestSuite.runTest(TestSuite.java:255) 
    at junit.framework.TestSuite.run(TestSuite.java:250) 
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:84) 
    at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:62) 
    at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:140) 
    at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:127) 
    at org.apache.maven.surefire.Surefire.run(Surefire.java:177) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
    at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:338) 
    at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:997) 
+0

发布异常跟踪 – Subin

+0

@Subin我编辑了问题并添加了异常跟踪。 –

+0

请看我的回答,是classpath中的hibernate.cfg.xml。 – Subin

回答

1

public Configuration configure() throws HibernateException Use the mappings and properties specified in an application resource named hibernate.cfg.xml. Throws: HibernateException

Configuration.configure()将正常工作只有hibernate.cfg.xml可作为应用程序资源。或类路径。

如果您正在使用Eclipse来调用JUnit测试,加上hibernate.cfg.xml的类路径,因为你正在使用maven你有它的src/main /资源

请参阅此链接查看,为什么java.lang.ClassFormatError:发生。 Maven: hibernate-entitymanager together with javaee-api break my unit tests

+0

记录实际异常我使用Netbeans 6.9来调用测试,是的,我在src/main/resources中配置了xml。 –

+0

它是否被命名为hibernate.cfg.xml? – Subin

+0

是的,它被命名为hibernate.cfg.xml。 此外,它正在正常运行该项目的工作完美。只有测试不起作用。 我编辑了问题以显示项目结构。 –