JPA_ユニットテストクラス
ユニットテストの时、面倒なのはいつも持続的な工场を创建して、まったく耐えられなくて、そこで、下のそれがあって、
具体的な実装:
/**
*
* @author hesheng
* , ,
*
*/
public abstract class BaseTestCase {
protected EntityManagerFactory emf;
protected EntityManager entityManager;
protected static boolean isOpenDB = true;
public boolean isOpenDB() {
return isOpenDB;
}
public void setOpenDB(boolean isOpenDB) {
this.isOpenDB = isOpenDB;
}
@SuppressWarnings("unchecked")
public void beginTx() {
emf = Persistence.createEntityManagerFactory("UnitName");
entityManager = emf.createEntityManager();
System.out.println("em: " + entityManager);
entityManager.getTransaction().begin();
}
public abstract void test();
public void endTx() {
entityManager.getTransaction().commit();
entityManager.close();
emf.close();
}
public void runTest(){
if(isOpenDB){
beginTx();
}
test();
if(isOpenDB){
endTx();
}
}
}
具体的な実装:
public class UnitTest extends BaseTestCase {
@Override
public void test() {
//getLogResultSet();
super.setOpenDB(false); // JPQL , , , ,
testSql(null,null);
}
public String testSql(String logTimeStart,String logTimeEnd){
String ql = "select m.pageName,count(m) as count from table m ";
int count = 0;
if (logTimeStart != null){
//ql = ql + " and";
count ++;
ql = ql + " where m.createDate >= ?" + count;
}
if (logTimeEnd != null){
ql = ql + " and";
count ++;
ql = ql + " m.createDate <= ?" + count;
}
ql = ql + " group by m.pageName";
System.out.println("ql: "+ql);
return ql;
}
public static void main(String[] args) {
new PageViewCountActionTest().runTest(); // ,in eclipse "run as Application"
}