2013-05-16 68 views
2

我有一個成熟的使用Hibernate/JPA的java應用程序,它工作得很好。我們正在嘗試添加一些單元/集成測試。我使用Spring的TestContext框架這樣做,我的測試類是這樣的:JPA實體在單元測試期間未被掃描

@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中粘貼所有必要的上下文配置。這包括上下文:組件掃描,定義dataSource,entityManagerFactory bean等。當應用程序運行時,我沒有錯誤。運行此測試時,我得到:

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

對於我嘗試查詢的任何和所有實體類。實體類用javax.persistence.Entity等註釋。

有誰知道爲什麼使用TestContext會失敗嗎?

更新

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

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

所以,我以爲這是自動掃描,以查找所有註釋的實體類。在運行單元測試時,在tomcat中部署的方式與文件夾結構之間的目錄結構有何不同?我們的persistence.xml是的WebContent/WEB-INF /班/ META-INF

而且 - 我注意到你可以定義 「packagesToScan」 使用Spring 3.1,但我們在使用Spring 3.0.6.RELEASE

回答

2

我得到了它運行通過Ant JUnit測試工作。我複製了persistence.xml文件以構建/ classes/META-INF,以便它與編譯的實體類位於同一個文件夾樹中,並且這允許掃描工作並且測試正確運行。

如果我刪除Persistence.xml的Webcontent/WEB-INF/classes/META-INF副本,並將其保留在構建/文件夾中,它將在Eclipse中起作用。我想知道是否有更好的方法可以在Eclipse中使用它。

3

此消息通常說,你還沒有被映射的實體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> 
+0

看到我上面的更新,persistence.xml是相當裸露的。在tomcat中部署,這工作得很好。 – NTyler

+0

不幸的是,我們正在使用3.0.6。我想知道是否有任何方法可以解決這個問題,而無需升級。 – NTyler

0

嘗試添加以下內容到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> 

我希望它會幫助你

+0

我欣賞這個幫助,但我想讓它自動掃描 - 我們有大量的實體。 – NTyler

+0

你試着指定你的春天上下文嗎?在@ContextConfiguration行上,你需要添加:({Spring-context})。所以結果是'@ContextConfiguration({Spring-context})' – mvlaicevich