簡體   English   中英

單元測試期間未掃描JPA實體

[英]JPA Entities not scanned during unit test

我有一個使用Hibernate / JPA的成熟的Java應用程序,可以正常工作。 我們正在嘗試添加一些單元/集成測試。 我正在使用Spring的TestContext框架執行此操作,我的測試類如下:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class ServiceTest {

    @Autowired
    private MyService myService;

    @Test
    public void testWorkspaceThing() throws Exception {

        List<MyEntity> entities = myService.someMethod();
        assertNotNull(entities);
    }
}

我將所有必要的上下文配置從應用程序上下文復制/粘貼到ServiceTest-context.xml中。 這包括context:component-scan,定義dataSource,entityManagerFactory bean等。當應用程序運行時,我沒有任何錯誤。 運行此測試時,我得到:

java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: MyEntity is not mapped.

對於所有實體類,我嘗試查詢。 Entity類使用javax.persistence.Entity等進行注釋。

有誰知道為什么使用TestContext失敗?

更新

我們的persistence.xml文件本質上是空的:

<persistence-unit name="some-pu" />

因此,我認為這是自動掃描以查找所有帶注釋的Entity類。 在運行單元測試時,目錄結構在tomcat中的部署方式與文件夾結構之間是否存在差異? 我們的persistence.xml位於WebContent / WEB-INF / classes / META-INF中

另外-我注意到您可以使用Spring 3.1定義“ packagesToScan”,但我們使用的是Spring 3.0.6.RELEASE

此消息通常表明您尚未在hibernate.cfg或persistence.xml中映射您的實體

<mapping class="com.yourpackage.MyEntity" />
...other entities

確保MyEntity類在其中映射,並且其在類中的注釋完全是@Entity(name="MyEntity")

編輯 :如果您使用的是上面的Spring 3.1,請嘗試通過以下命令在測試上下文中指示EntityManager工廠bean:

  <property name="packagesToScan">
     <list>
        <value>com.yourpackage.domain</value>
     </list>
  </property>

我通過通過螞蟻運行junit測試使其工作。 我將persistence.xml文件復制到build / classes / META-INF,以便它與已編譯實體類位於同一文件夾樹中,這使掃描能夠正常進行並且測試能夠正確運行。

如果我刪除persistence.xml的Webcontent / WEB-INF / classes / META-INF副本並將其保留在build /文件夾中,則這在Eclipse中有效。 我想知道是否有更好的方法可以使其在Eclipse中工作。

嘗試將其添加到您的persistence.xml文件中。

<persistence-unit name="some-pu" transaction-type="RESOURCE_LOCAL">
<class>package.MyEntity</class>

        <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
        <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
        <property name="hibernate.connection.username" value="USER-DB"/>
        <property name="hibernate.connection.password" value="PASSWORD-DB"/>
        <property name="hibernate.connection.url" value="jdbc:mysql://SERVER-IP/some-pu?autoReconnect=true"/>
        <property name="hibernate.max_fetch_depth" value="3"/>

         <!-- Determines how many connections at a time c3p0 will try to acquire when the pool is exhausted. :: default=1 -->
        <property name="connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider"/>
        <property name="hibernate.c3p0.min_size" value="2"/>
        <property name="hibernate.c3p0.timeout" value="300"/>
        <property name="hibernate.c3p0.idle_test_period" value="300"/>
        <property name="hibernate.c3p0.acquire_increment" value="1"/> 
        <property name="hibernate.c3p0.max_size" value="10"/> 
        <property name="hibernate.c3p0.max_statements" value="50"/>     

    </properties>
</persistence-unit>

希望對您有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM