java - JPA 单元测试 - 查询返回比以往任何时候都多的实体

标签 java unit-testing jpa junit openjpa

我写了一堆实体。我现在正在尝试测试它们。我遇到了一个与此非常相似的实体的问题,但是当我清理构建时问题突然消失了。然后我开始为 SetMap 写一个类似的测试,然后......同样的问题。我 99.9999% 确定我只坚持 100 个 SetMaps。然而,我的测试表明在下面的代码中有 101 个查询。

  1. 我觉得这些测试很糟糕。有人可以给我一些关于单元测试实体类的指示吗?我到底应该测试什么?只是在我坚持之后才存在?

  2. 为什么,为什么,为什么我下面的测试方法中返回了101个SetMaps。怎么……为什么……=-[

  3. 每次编译都会出现这个错误:

    89 persistence-test WARN [main] openjpa.Runtime - 在以下资源中多次发现持久性单元“persistence-test”“[file:/home/matt/Code/vox/vox-lib/vox- lib-common/vox-entity/target/classes/META-INF/persistence.xml,文件:/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 持久性测试信息 [main] openjpa.jdbc.JDBC - 使用字典类“org.apache.openjpa.jdbc.sql.DerbyDictionary”。

我的 src/main/resources/META-INF 文件夹中肯定只有一个 persistence.xml。 我正在使用行家。 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;
}

请帮忙!

最佳答案

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

谁说没有? Maven说有一个 /home/matt/Code/vox/vox-lib/vox-lib-common/vox-entity/target/classes/META-INF/persistence.xml

还有一个 /home/matt/Code/vox/vox-lib/vox-lib-common/vox-entity/src/main/resources/META-INF/persistence.xml

并且这两个都在 runtime 的 CLASSPATH 中(此时您没有编译,也许“编译”是调用的目标之一,但这不在该步骤中)。 因此,修复您的 pom.xml,使其在 Maven 为您执行某些操作时不复制任何内容。那将(应该)然后删除错误消息

关于java - JPA 单元测试 - 查询返回比以往任何时候都多的实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8867439/

相关文章:

java - 通过套接字发送图像

Java固定文件大小操作

android - 我如何模拟和测试这个类?

hibernate - JPQL 中的索引元素访问

java - 如何在另一个类中使用一个类的方法而不扩展

java - 检查Firebase数据库中是否存在值

java - 如何测试具有私有(private)方法、字段或内部类的类?

unit-testing - 如何在 Go 中打印数组项的类型?

java - 表 'customerjpa.sequence' 不存在 JPA

mysql - 如何配置 TomEE 和 JPA