我有以下测试方法(基于以前创建和工作的测试用例):
'公共类 CommentGenericDaoHibernateImplTest { 私人评论评论; 私人 ReqCandAssociation reqCandAssc; 私人工作要求 工作要求; 私人候选人;
private ClassPathXmlApplicationContext ctx;
private GenericDao<Comment, Integer> dao;
private GenericDao<JobReq, Integer> reqDao;
private GenericDao<Candidate, Integer> candDao;
private GenericDao<ReqCandAssociation, Integer> reqCandDao;
@SuppressWarnings("unchecked")
@Before
public void setUp() {
JOBREQ = new JobReq("TTYL", "Title", "contract", "project", "laborCategory", "summary", "jobDescription", "status", "TTONumber", new Date(), new Date(), "location", "lead", "leadEmail", "reason", "duration", "recruiter", "openingType", new Date(), "f", "2");
CANDIDATE = new Candidate("candName", "email", "phoneNum", "title", "mainStatus", new Date(), new Date(), new Date(), "none", "skillsSummary", "costRate", "clearanceType", new Date(), "contingentHire", "submittingCompany", "employingCompany", "5");
reqCandAssc = new ReqCandAssociation(CANDIDATE, JOBREQ, "Hired");
comment = new Comment("CommentGenericDaoHibernateImplTest TEST COMMENT", new Date(), reqCandAssc);
ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
reqDao = (GenericDaoHibernateImpl<JobReq, Integer>) ctx.getBean("jobReqDao");
candDao = (GenericDaoHibernateImpl<Candidate, Integer>) ctx.getBean("candDao");
reqCandDao = (GenericDaoHibernateImpl<ReqCandAssociation, Integer>) ctx.getBean("reqCandDao");
dao = (GenericDaoHibernateImpl<Comment, Integer>) ctx.getBean("commentDao");
reqDao.create(JOBREQ);
candDao.create(CANDIDATE);
reqCandDao.create(reqCandAssc);
}
@Test
public void testCreatingComment() {
try {
Integer key = reqDao.create(comment);
assertTrue("attachment not created successfully!", key > 0);
} finally {
if (ctx != null) {
ctx.close();
}
}
}
@Test
public void testRetrievingCommentByIdAfterCreate() {
try {
Integer key = dao.create(comment);
Comment retrievedComment = dao.read(key);
assertEquals("Could not find attachment with id = "+key, retrievedComment, comment);
} finally {
if (ctx != null) {
ctx.close();
}
}
}
@Test
public void testUpdatingCommentAfterCreate() {
try {
Integer key = dao.create(comment);
comment.setComment("ALTERED");
dao.update(comment);
Comment retrievedComment = dao.read(key);
assertEquals("Could not update attachment with id = "+key,
comment.getComment(), retrievedComment.getComment());
} finally {
if (ctx != null) {
ctx.close();
}
}
}
@Test
public void testDeletingCommentAfterCreate() {
try {
comment.setCommentDate(new Date());
Integer key = dao.create(comment);
assertTrue("attachment not created successfully!", key > 0);
dao.delete(comment);
Comment queriedComment = dao.read(key);
assertNull("Could not delete attachment with id = "+key, queriedComment);
} finally {
if (ctx != null) {
ctx.close();
}
}
}
@Test
public void testRetrievingAllCommentsAfterCreate() {
try {
Integer key = dao.create(comment);
assertTrue("attachment not created successfully!", key > 0);
List<Comment> queriedAttachmentList = dao.findAll();
assertEquals("Could not list all attachment", 1, queriedAttachmentList.size());
} finally {
if (ctx != null) {
ctx.close();
}
}
}
}'
它在这样的方法中使用休眠:
@SuppressWarnings("unchecked")
public PK create(T o) {
Session hibernateSession = this.getSession();
PK id = null;
try {
hibernateSession.beginTransaction();
id = (PK) hibernateSession.save(o);
hibernateSession.getTransaction().commit();
} finally {
hibernateSession.close();
}
return id;
}
对数据库执行操作。 现在,Comment
实体位于具有 ReqCandAssociation 的 OneToMany 中,而 ReqCandAssociation 又是 JobReq 和 Candidate 的 ManyToMany 解析表。 所以基本上我需要创建一个 JobReq, Candidate ,然后我可以创建一个 ReqCandAssociation,然后创建一个注释以附加到一个 ReqCandAssociation 对象。 但是,每当我尝试将多个对象添加到数据库中时,我都会Waiting for table metadata lock
MySQL 中的进程。
尝试测试一次创建一个实体,它允许我创建一个候选人或一个 jobreq 并添加一个 jobreq(而不是评论,用于测试),但是一旦我尝试同时添加候选人和工作要求,然后做其他事情我得到锁。 我还尝试在@Test
方法本身而不是安装程序中添加这些先决条件对象。
经过相当多的挖掘后发现,如果您的测试抛出错误,它会锁定数据库,出于某种原因,这些 Junit 测试不会像普通类那样显示语法错误突出显示,并且(似乎)编译良好。 我的一个对象构造函数中的值放错了位置,导致错误。
在大量战略性地放置System.out.println()之后弄清楚了这一点