测试套件在eclipse中运行良好。然而,当我们试图用ANT运行套装时,它会抛出以下错误:
Warning: Caught exception attempting to use SAX to load a SAX XMLReader
Warning: Exception was: java.lang.ClassCastException: org.apache.xerces.parsers.SAXParser cannot be cast to org.xml.sax.XMLReader
Warning: I will print the stack trace then carry on using the default SAX parser
java.lang.ClassCastException: org.apache.xerces.parsers.SAXParser cannot be cast to org.xml.sax.XMLReader
at org.xml.sax.helpers.XMLReaderFactory.loadClass(XMLReaderFactory.java:199)
at org.xml.sax.helpers.XMLReaderFactory.createXMLReader(XMLReaderFactory.java:150)
at org.dom4j.io.SAXHelper.createXMLReader(SAXHelper.java:83)
at org.dom4j.io.SAXReader.createXMLReader(SAXReader.java:894)
at org.dom4j.io.SAXReader.getXMLReader(SAXReader.java:715)
at org.dom4j.io.SAXReader.read(SAXReader.java:435)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1481)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1425)
正如许多人在网上建议的那样,我们试图忽略这些类,比如:
@PrepareForTest({MessageUtils.class, FacesUtils.class})
@PowerMockIgnore( { "com.sun.org.apache.xerces.*", "org.dom4j.*", "org.xml.sax.*" })
public class FooTests extends PowerMockTestCase {}
这并没有解决问题,我们开始得到这个错误:
java.lang.ExceptionInInitializerError
at com.magnus.newpack.dao.common.SessionManager.<clinit>(SessionManager.java:19)
at com.magnus.newpack.dao.common.DAOBase.getSession(DAOBase.java:174)
at com.magnus.newpack.dao.common.DAOBase.beginTransaction(DAOBase.java:63)
at com.magnus.newpack.dao.PackagesDAO.getPackages(PackagesDAO.java:59)
at com.magnus.newpack.managers.CreateNewUserManager.getPackages(CreateNewUserManager.java:23)
at com.magnus.webapp.action.admin.createUserPage.clearValues(createUserPage.java:181)
at com.magnus.webapp.action.admin.createUserPage.<init>(createUserPage.java:130)
at com.magnus.service.CreateUserServiceClass.<init>(CreateUserServiceClass.java:36)
at com.magnus.service.CreateUserServiceErrorsTests.testCreateUserNoNameNoFacesContext(CreateUserServiceErrorsTests.java:79)
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.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:691)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:883)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1208)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:758)
at org.testng.TestRunner.run(TestRunner.java:613)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:87)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1137)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1062)
at org.testng.TestNG.run(TestNG.java:974)
at org.testng.TestNG.privateMain(TestNG.java:1282)
at org.testng.TestNG.main(TestNG.java:1246)
Caused by: org.hibernate.HibernateException: Could not instantiate dialect class
at org.hibernate.dialect.DialectFactory.buildDialect(DialectFactory.java:84)
at org.hibernate.dialect.DialectFactory.buildDialect(DialectFactory.java:42)
at org.hibernate.cfg.SettingsFactory.determineDialect(SettingsFactory.java:409)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:119)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2006)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1289)
at com.magnus.newpack.dao.common.SessionManager.<clinit>(SessionManager.java:14)
... 31 more
Caused by: java.lang.ClassCastException: org.hibernate.dialect.MySQLDialect cannot be cast to org.hibernate.dialect.Dialect
at org.hibernate.dialect.DialectFactory.buildDialect(DialectFactory.java:78)
... 37 more
在玩了一段时间之前的例子之后,我们似乎只是在把问题推给其他代码来处理。我们也一直在玩如何加载罐子,这样我们就可以解决任何冲突,但到目前为止我们还没有运气。任何人能提供的帮助都会很棒。
我也遇到了类似的问题。对我有效的是将fork="true"
设置为junit任务。