我已經寫了一堆實體。我正試圖現在測試它們。我遇到了與此非常類似的實體問題,但是當我清理構建時,突然間問題消失了。然後我開始爲SetMap寫一個類似的測試,然後......同樣的問題。我99.9999%肯定我只有100個SetMaps。但是,我的測試表明在下面的代碼中有101個查詢。JPA單元測試 - 返回比以往更多的實體的查詢
我覺得這些測試很糟糕。有人可以給我一些關於單元測試實體類的指針嗎?我應該測試什麼?僅僅在我堅持他們之後存在?
爲什麼爲什麼有101個SetMaps在我的測試方法下面返回。如何...爲什麼... = - [
我得到這個錯誤每次我編譯:
89的持久性測試警告[主] openjpa.Runtime - 持久性單元「持久性測試」被發現多次在以下資源中「[file:/home/matt/Code/vox/vox-lib/vox-lib-common/vox-entity/target/classes/META-INF/persistence.xml,file:/ home/matt/Code/vox/vox-lib/vox-lib-common/vox-entity/src/main/resources/META-INF/persistence.xml]「,但持久性單元名稱應該是唯一的。正在使用與「file:/home/matt/Code/vox/vox-lib/vox-lib-common/vox-entity/target/classes/META-INF/persistence.xml」中提供的名稱匹配的第一個持久性單元。 202 persistence-test INFO [main] openjpa.Runtime - 啓動OpenJPA 2.1.1 448 persistence-test INFO [main] openjpa.jdbc.JDBC - 使用字典類「org.apache.openjpa.jdbc.sql.DerbyDictionary」。
我的src/main/resources/META-INF文件夾裏肯定只有一個persistence.xml。 我正在使用maven。 maven是否有可能在某處複製某些內容?
public class SetMapTest {
private static EntityManagerFactory emf;
private static EntityManager em;
private static EntityTransaction tx;
@BeforeClass
public static void initEntityManager() throws Exception {
emf = Persistence.createEntityManagerFactory("persistence-test");
em = emf.createEntityManager();
}
@AfterClass
public static void closeEntityManager() throws SQLException {
em.close();
emf.close();
}
@Before
public void initTransaction() {
tx = em.getTransaction();
}
@Test
public void testSomeMethod() {
// check that nothing is there
String s = "SELECT x FROM SetMap x";
Query q = em.createQuery(s);
List<SetMap> qr = q.getResultList();
assertEquals(0, qr.size());
int count = 100;
// util method that returns setMaps where setMap.title = 'title:i'
// where 0 <= i < count
ArrayList<SetMap> setMaps = TestUtil.getNumberedSetMapList(count);
assertEquals(setMaps.size(), count);
VoxUser author = TestUtil.getNumberedVoxUser(0);
SetPatternMap setPatternMap = TestUtil.getNumberedSetPatternMap(0);
setPatternMap.setAuthor(author);
author.addSetPatternMap(setPatternMap);
tx.begin();
em.persist(author);
em.persist(setPatternMap);
Iterator<SetMap> iter = setMaps.iterator();
while(iter.hasNext()){
SetMap setMap = iter.next();
em.persist(setMap);
setMap.setAuthor(author);
author.addSetMap(setMap);
setMap.setSetPatternMap(setPatternMap);
setPatternMap.addSetMap(setMap);
}
tx.commit();
String qString = "SELECT x FROM SetMap x";
q = em.createQuery(qString);
qr = q.getResultList();
assertEquals(count, qr.size()); // this is the method that fails
// it claims there are 101 SetMaps in the P.C.
for (int i = 0; i < 1; i++) {
String qt = "SELECT x FROM SetMap x WHERE x.title = 'title:" + i + "'";
q = em.createQuery(qt);
qr = q.getResultList();
assertEquals(1, qr.size());
// i played around with this a little bit. It seems there are two SetMaps
// whose titles are "title:0" I can't figure out how that is....
}
}
}
我的一些UTIL方法:
public static PhraseMap getNumberedPhraseMap(int pos){
PhraseMap phraseMap = new PhraseMap();
phraseMap.setTitle("title:" + pos);
return phraseMap;
}
public static ArrayList<PhraseMap> getNumberedPhraseMapList(int count){
ArrayList<PhraseMap> phraseMaps = new ArrayList<PhraseMap>();
for(int i = 0; i < count; i++){
PhraseMap phraseMap = getNumberedPhraseMap(i);
phraseMaps.add(phraseMap);
}
return phraseMaps;
}
幫助請!
您是否包含所有@Test方法?所以沒有更多的測試干擾'testSomeMethod'。如果將計數設置爲1,會發生什麼情況?你最終有兩個實體嗎? – DagR 2012-01-15 07:01:05
testSomeMethod()是SetMapTest中唯一的測試方法,但還有一些測試類測試其他實體。我的印象是,這些其他測試類在完全獨立的數據庫實例上運行,所以另一個測試類不可能干擾?它是否正確?即便如此,沒有其他類創建額外的SetMap。另外,如果我將count設置爲1,則創建兩個SetMap實體。 – b3bop 2012-01-15 08:35:56