English 中文(简体)
JPA 单位测试——返回的实体比以往任何时候都多
原标题:JPA Unit testing - Query returning more entities than were ever persisted

我写了一批实体。 我现在试图检验他们。 我与一个非常相似的实体存在问题,但当我清理大楼时,这个问题突然消失。 然后,我开始撰写“SetaMap”的类似试验,然后是......。 我有99.9999%确信,我只坚持100 SetMaps。 然而,我的检验表明,在以下法典中有101个问题。

  1. 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?

  2. 因此,为什么有101个标准标准回到了我下面的测试方法中。 ......为什么......

  3. 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

最佳回答

there is definitely only one persistence.xml in my src/main/resources/META-INF folder

Who said there wasn t ? Maven says there is one in /home/matt/Code/vox/vox-lib/vox-lib-common/vox-entity/target/classes/META-INF/persistence.xml

and one in /home/matt/Code/vox/vox-lib/vox-lib-common/vox-entity/src/main/resources/META-INF/persistence.xml

and both of these are in the CLASSPATH at runtime (you are not compiling at that point, maybe "compile" was one of the goals invoked but this is not in that step). So fix your pom.xml to not copy things around when Maven is doing some things for you. That would (should) then remove the error message

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签