我有以下测试方法(基于以前创建和工作的测试用例):
`public class CommentGenericDaoHibernateImplTest { 私人评论评论; private ReqCandAssociation reqCandAssc; 私人JobReq JOBREQ; 私人候选人候选人;
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();
}
}
}
}`
在这样的方法中使用hibernate:
@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;
}
对DB采取行动。现在,Comment
实体位于具有ReqCandAssociation的OneToMany中,而ReqCandAssociation又是JobReq和Candidate的ManyToMany解析表。所以基本上我需要创建一个JobReq,Candidate,然后我可以创建一个ReqCandAssociation,然后一个注释附加到One ReqCandAssociation对象。但是,每当我尝试向数据库添加多个对象时,我就会在MySQL的进程中获得Waiting for table metadata lock
。
我尝试过一次测试创建一个实体,它允许我制作一个候选人或一个jobreq并添加一个jobreq(而不是一个评论,用于测试)但是一旦我尝试添加一个候选人和一个工作req然后做别的事我得到了锁。我也尝试在@Test
方法本身而不是在设置中添加这些先决条件对象。
答案 0 :(得分:0)
经过相当多的挖掘后发现,如果你的测试抛出一个错误它会锁定数据库,由于某种原因,这些Junit测试没有显示语法错误突出显示正常类和(似乎)编译正常。我的一个对象构造函数中的错误值导致了错误。
在经过大量策略性放置System.out.println()的
之后想出来了