使用 Junit 运行 maven 构建时,出现“没有命名实体管理器的持久性提供程序”错误。无法识别我的代码中缺少的内容。
主类:
public class ApprovalHistory {
@PersistenceContext(unitName = "Approval_History")
private Logger logger = LoggerFactory.getLogger(ApprovalHistory.class);
public EntityManagerFactory emfactory = null;
final String JDBC_URL_H2DB = "jdbc:h2:file:./APApproval/ApprovalHistoryH2DB";
final String JDBC_USERNAME_H2DB = "";
final String JDBC_PASSWORD_H2DB = "";
final String JDBC_DRIVER_SQL = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
final String JDBC_DRIVER_H2 = "org.h2.Driver";
final String JDBC_DRIVER_HSQL = "org.hsqldb.jdbc.JDBCDriver";
final String PERSISTANCE_UNIT_NAME = "Approval_History";
final String SELECT_BO_TABLE = "SELECT bobj FROM BusinessObjectTable bobj where bobj.docId =:";
final String SELECT_COMMENT_TABLE = "SELECT comment FROM CommentTable comment where comment.actionTable.id IN :";
final String SELECT_REASON_TABLE = "SELECT reason FROM ReasonTable reason where reason.actionTable.id IN :";
public ApprovalHistory()
{
try {
createEntityManagerFactory();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void insertData(List<ApprovalHistoryModel> historyList){
if(historyList != null && !historyList.isEmpty())
{
EntityManager entitymanager = null;
try
{
entitymanager = getEntityManager();
entitymanager.getTransaction().begin();
ApprovalHistoryModel firstItem = historyList.get(0);
ActionTable a = new ActionTable(firstItem.actionType, firstItem.tenantId, firstItem.comment, firstItem.reason);
for(ApprovalHistoryModel h : historyList)
{
a.getBusinessObjects().add(new BusinessObjectTable(h.userName, h.taskId, h.docId, h.objectIdentifier1, h.objectIdentifier2, h.objectIdentifier3,h.tenantId));
}
entitymanager.persist(a);
entitymanager.getTransaction().commit();
}
catch (RuntimeException e) {
if(entitymanager!=null && entitymanager.getTransaction().isActive()) {
entitymanager.getTransaction().rollback();
}
throw e;
}
finally{
closeEntityManager(entitymanager);
}
}
}
public List<ApprovalHistoryModel> getApprovalHistory(String docID) throws Exception
{
logger.info("=ApprovalConnector=: start of getApprovalHistory()");
List<ApprovalHistoryModel> historyModels = new ArrayList<ApprovalHistoryModel>();
if(docID!=null && !docID.isEmpty()) {
EntityManager entitymanager = null;
try
{
entitymanager = getEntityManager();
TypedQuery<BusinessObjectTable> bobjquery = entitymanager.createQuery(SELECT_BO_TABLE+"DocID ",BusinessObjectTable.class);
bobjquery.setParameter("DocID", docID);
List<BusinessObjectTable> bobjs = bobjquery.getResultList();
if(bobjs!=null){
for (BusinessObjectTable bobj : bobjs) {
ActionTable a = bobj.getActionTable();
ApprovalHistoryModel history = new ApprovalHistoryModel();
history.docId = bobj.getDocId();
history.taskId = bobj.getApprovalItemId();
history.userName = bobj.getUserName();
logger.debug("=ApprovalConnector=: getApprovalHistory(): documentID - "+bobj.getDocId());
history.actionType = a.getActionType();
logger.debug("=ApprovalConnector=: getApprovalHistory(): actionType - "+ history.actionType);
history.actionDate = ISODateTimeFormat.dateTime().print(new DateTime(a.getActionDate()));
history.objectIdentifier1 = bobj.getObjectIdentifier1();
history.objectIdentifier2=bobj.getObjectIdentifier2();
history.objectIdentifier3 = bobj.getObjectIdentifier3();
history.tenantId = a.getTenantId();
history.comment = a.getComment()!=null?a.getComment().getComment():"";
history.reason = a.getReason()!=null?a.getReason().getReason():"";
historyModels.add(history);
}
}
logger.info("=ApprovalConnector=: end of getApprovalHistory()");
}
finally{
closeEntityManager(entitymanager);
}
}
return historyModels;
}
public void createEntityManagerFactory() throws Exception
{
Map<String, String> persistenceMap = new HashMap<String, String>();
String jdbcDriver = getJdbcDriverName(JDBC_URL_H2DB);
persistenceMap.put("javax.persistence.jdbc.driver", jdbcDriver);
persistenceMap.put("javax.persistence.jdbc.url", JDBC_URL_H2DB);
if (!JDBC_USERNAME_H2DB.isEmpty()) {
persistenceMap.put("javax.persistence.jdbc.user", JDBC_USERNAME_H2DB);
}
if (!JDBC_PASSWORD_H2DB.isEmpty()) {
persistenceMap.put("javax.persistence.jdbc.password", JDBC_PASSWORD_H2DB);
}
persistenceMap.put("eclipselink.session-name",System.currentTimeMillis() + "");
this.emfactory = Persistence.createEntityManagerFactory(PERSISTANCE_UNIT_NAME, persistenceMap);
}
public EntityManager getEntityManager()
{
EntityManager entitymanager = this.emfactory.createEntityManager();
return entitymanager;
}
public void closeEntityManager(EntityManager entitymanager)
{
if(entitymanager!=null)
entitymanager.close();
}
public void closeEntityManagerFactory()
{
if(this.emfactory!=null)
this.emfactory.close();
}
private String getJdbcDriverName(String jdbcUrl) {
if (jdbcUrl.startsWith("jdbc:sqlserver"))
return JDBC_DRIVER_SQL;
if (jdbcUrl.startsWith("jdbc:h2"))
return JDBC_DRIVER_H2;
if (jdbcUrl.startsWith("jdbc:hsqldb"))
return JDBC_DRIVER_HSQL;
return null;
}
Test Calss:
public class ApprovalHistoryTest {
ApprovalHistory approvalHistory = new ApprovalHistory();
@Before
public void setUp() throws Exception {
List<ApprovalHistoryModel> actionHistoryModels = new ArrayList<ApprovalHistoryModel>();
for(int i=0;i<=2;i++){
ApprovalHistoryModel history = new ApprovalHistoryModel();
String comment = "comment no. " + i;
String reason = "reason no. " + i;
String userName = "User" + i;
history.taskId = "321YZ61_0026CV7Z0000XB" + i;
history.actionDate = ISODateTimeFormat.dateTime().print(new DateTime(new Date()));
history.actionType = i;
history.comment = comment.trim();
history.docId = "321YZ61_026CV7Z0000TD" + i;
history.userName = userName;
history.reason = reason;
actionHistoryModels.add(history);
}
approvalHistory.insertData(actionHistoryModels);
}
@After
public void tearDown() throws Exception {
DeleteApprovalHistory history = new DeleteApprovalHistory();
try{
history.purgeRecord(0,"DAYS");
approvalHistory.closeEntityManagerFactory();
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test()
public void test() {
//ApprovalHistory approvalHistory = new ApprovalHistory();
List<ApprovalHistoryModel> historyList = new ArrayList<ApprovalHistoryModel>();
for(int i=0;i<=2;i++){
ApprovalHistoryModel history = new ApprovalHistoryModel();
try {
Thread.sleep(1000);
historyList=approvalHistory.getApprovalHistory(history.docId);
assertEquals("321YZ61_0026CV7Z0000XB" + i,historyList.get(i).taskId);`enter code here`
assertEquals("comment no. " + i,historyList.get(i).comment);
assertEquals("User" + i,historyList.get(i).userName);
assertEquals("reason no. " + i,historyList.get(i).reason);
assertEquals(i,historyList.get(i).actionType);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="Approval_History" transaction-type="RESOURCE_LOCAL">
<class>com.perceptivesoftware.apapproval.history.ActionTable</class>
<class>com.perceptivesoftware.apapproval.history.BusinessObjectTable</class>
<class>com.perceptivesoftware.apapproval.history.CommentTable</class>
<class>com.perceptivesoftware.apapproval.history.ReasonTable</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<properties>
<property name="eclipselink.ddl-generation" value="create-or-extend-tables" />
<property name="eclipselink.logging.level.sql" value="FINE"/>
<property name="eclipselink.logging.parameters" value="true"/>
<property name="eclipselink.multitenant.tenants-share-cache" value="true" />
</properties>
</persistence-unit>
</persistence>
错误::
运行 ApprovalHistoryTest javax.persistence.PersistenceException:在 com.perceptivesoftware.apapproval.history.ApprovalHistory.createEntityManagerFactory(ApprovalHistory.java:167) 在 com.perceptivesoftware .apapproval.history.ApprovalHistory.(ApprovalHistory.java:48) 在 ApprovalHistoryTest.(ApprovalHistoryTest.java:20) 在 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 在 sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:422) at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:187) at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:236) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java :15) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68) 在 org.junit.runners.BlockJUnit4ClassRunner.java:233) java:47) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) at org.junit.runners.ParentRunner.runChildren (ParentRunner.java:229)在 org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)在 org.junit。runners.ParentRunner$2.evaluate(ParentRunner.java:222) at org.junit.runners.ParentRunner.run(ParentRunner.java:300) at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252 ) 在 org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112) 在 sun.reflect.NativeMethodAccessorImpl 的 org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)。 invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java: 497) 在 org.apache.maven.surefire.booter 的 org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)。ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165) at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85) at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter. java:115) 在 org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75) javax.persistence.PersistenceException:在 javax.persistence.Persistence.createEntityManagerFactory 中没有名为 Approval_History 的 EntityManager 的持久性提供程序(未知来源)在 com.perceptivesoftware.apapproval.history.ApprovalHistory.createEntityManagerFactory(ApprovalHistory.java:167) 在 com.perceptivesoftware.apapproval.history.ApprovalHistory.(ApprovalHistory.java:48) 在 com.perceptivesoftware.apapproval.history.DeleteApprovalHistory.purgeRecord(删除ApprovalHistory.java:46) 在 Sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl) 的 ApprovalHistoryTest.tearDown(ApprovalHistoryTest.java:50) .java:43) 在 java.lang.reflect.Method.invoke(Method.java:497) 在 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45) 在 org.junit.internal.runners .model.ReflectiveCallable.run(ReflectiveCallable.java:15) 在 org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42) 在 org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java :36) 在 org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263) 在 org.junit.runners.BlockJUnit4ClassRunner。runChild(BlockJUnit4ClassRunner.java:68) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) at org.junit.runners。 ParentRunner$1.schedule(ParentRunner.java:60) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) at org.junit .runners.ParentRunner$2.evaluate(ParentRunner.java:222) at org.junit.runners.ParentRunner.run(ParentRunner.java:300) at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java: 252) 在 org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112) 在 sun.reflect 的 org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)。NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.lang.reflect.Method.invoke(Method. java:497) 在 org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189) 在 org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165) 在 org .apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85) 在 org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115) 在 org.apache.maven.surefire.booter .ForkedBooter.main(ForkedBooter.java:75)invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at org.apache.maven.surefire.util .ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189) 在 org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165) 在 org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory .java:85) 在 org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115) 在 org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at org.apache.maven.surefire.util .ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189) 在 org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165) 在 org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory .java:85) 在 org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115) 在 org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)497) 在 org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165) 在 org.apache. .maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85) 在 org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115) 在 org.apache.maven.surefire.booter.ForkedBooter .main(ForkedBooter.java:75)497) 在 org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165) 在 org.apache. .maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85) 在 org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115) 在 org.apache.maven.surefire.booter.ForkedBooter .main(ForkedBooter.java:75)runSuitesInProcess(ForkedBooter.java:115) 在 org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)runSuitesInProcess(ForkedBooter.java:115) 在 org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)