我写了一批实体。 我现在试图检验他们。 我与一个非常相似的实体存在问题,但当我清理大楼时,这个问题突然消失。 然后,我开始撰写“SetaMap”的类似试验,然后是......。 我有99.9999%确信,我只坚持100 SetMaps。 然而,我的检验表明,在以下法典中有101个问题。
I feel like these tests suck. Can someone give me some pointers on unit testing Entity classes? what exactly should I be testing for? merely existing after I persist them?
因此,为什么有101个标准标准回到了我下面的测试方法中。 ......为什么......
I get this error everytime I compile:
89 persistence-test WARN [main] openjpa.Runtime - The persistence unit "persistence-test" was found multiple times in the following resources "[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]", but persistence unit names should be unique. The first persistence unit matching the provided name in "file:/home/matt/Code/vox/vox-lib/vox-lib-common/vox-entity/target/classes/META-INF/persistence.xml" is being used. 202 persistence-test INFO [main] openjpa.Runtime - Starting OpenJPA 2.1.1 448 persistence-test INFO [main] openjpa.jdbc.JDBC - Using dictionary class "org.apache.openjpa.jdbc.sql.DerbyDictionary".
there is definitely only one persistence.xml in my src/main/resources/META-INF folder. I am using maven. Is it possible maven is copying something somewhere?
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....
}
}
}
我的一些实用方法:
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;
}
页: 1