隨着SpringJUnit4ClassRunner
,JUnit 4中和Spring測試我寫單元測試Service
它使用了Spring數據JPA Repository
和嵌入式HSQL數據庫:彈簧試驗使用JUnit 4和Spring數據JPA:的NoSuchMethodError org.hibernate.engine.spi.SessionFactoryImplementor.getProperties
@Ignore
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath*:unitTestFullConfig.xml")
public class InMemoryDBFullTestBaseClass {
}
public final class ActorServiceImplTest extends InMemoryDBFullTestBaseClass {
@Inject
private ActorService service;
@Test
public final void saveActor() throws Exception {
service.save(new ActorDTO(null, "testName", "testSurname", new Date(), Collections.emptyList()));
assertEquals(1, service.getAll().size());
}
}
我跑測試與required javaagent option on VM,與配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Configure the data source bean -->
<jdbc:embedded-database id="dataSource" type="HSQL">
</jdbc:embedded-database>
<!-- Enable annotation driven transaction management -->
<tx:annotation-driven/>
<mvc:annotation-driven/>
<context:component-scan base-package="beans"/>
<!-- Create default configuration for Hibernate -->
<bean id="hibernateJpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
<!-- Configure the entity manager factory bean -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/>
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
</property>
<!-- Set JPA properties -->
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="javax.persistence.schema-generation.database.action">none</prop>
<prop key="hibernate.ejb.use_class_enhancer">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<!-- Set base package of your entities -->
<property name="packagesToScan" value="models"/>
<!-- Set share cache mode -->
<property name="sharedCacheMode" value="ENABLE_SELECTIVE"/>
<!-- Set validation mode -->
<property name="validationMode" value="NONE"/>
<property name="persistenceUnitName" value="testJPA" />
</bean>
<!-- Configure the transaction manager bean -->
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<!--
Configure Spring Data JPA and set the base package of the
repository interfaces
-->
<jpa:repositories base-package="beans.repositories"/>
</beans>
但我得到:
Error creating bean with name 'entityManagerFactory' defined in URL [file:/E:/workspace/film-site/out/test/main/unitTestFullConfig.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: org.hibernate.engine.spi.SessionFactoryImplementor.getProperties()Ljava/util/Properties; at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean...
測試配置和applicationContext.xml
(其中工程爲Tomcat應用程序)之間的唯一區別是嵌入在測試中使用的數據庫,但即使我用dataSource
從項目:
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="org.postgresql.Driver"/>
<property name="jdbcUrl" value="jdbc:postgresql://localhost:5432/film-site"/>
<property name="user" value="postgres"/>
<property name="password" value="postgres"/>
<property name="maxPoolSize" value="10"/>
<property name="maxStatements" value="0"/>
<property name="minPoolSize" value="5"/>
</bean>
我仍然面臨着同樣的問題(項目正常工作)。另外,我不認爲問題是我沒有hibernate.properties
文件,因爲我在這裏詢問了它:Spring Data configuration - hibernate.properties not found。我用Spring 4.3.2.RELEASE
,Hibernate Core 5.2.0.Final
,Hibernate Entity Manager 5.1.0.Final
,Spring Data 1.10.2.RELEASE JPA
,Spring Data Commons 1.12.2.RELEASE
和Spring Data Commons Core 1.4.1.RELEASE
。如果有人能幫助我,我會很高興 - 事先感謝您。 更新:我改變jpaProperties
到jpaPropertyMap
爲entityManagerFactory
:
<property name="jpaPropertyMap">
<map>
<entry key="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
<entry key="javax.persistence.schema-generation.database.action" value="none" />
<entry key="hibernate.ejb.use_class_enhancer" value="true" />
<entry key="hibernate.hbm2ddl.auto" value="create" />
<entry key="hibernate.show_sql" value="true" />
</map>
</property>
兼評依賴於hibernate-entitymanager
,但它仍然無法正常工作。我也有同樣的問題,當我切換到休眠5.1 更新2:我創建一個Java版本的配置,也許這將幫助別人看到我犯了一個錯誤:
package config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import javax.persistence.EntityManagerFactory;
import javax.persistence.SharedCacheMode;
import javax.persistence.ValidationMode;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
@Configuration
public class HibernateConfig {
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL).build();
}
// Create default configuration for Hibernate
@Bean
public JpaVendorAdapter jpaVendorAdapter() {
return new HibernateJpaVendorAdapter();
}
// Configure the entity manager factory bean
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setDataSource(dataSource());
factory.setJpaVendorAdapter(jpaVendorAdapter());
factory.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver());
factory.setJpaPropertyMap(createJpaProperties());
factory.setPackagesToScan("models");
factory.setSharedCacheMode(SharedCacheMode.ENABLE_SELECTIVE);
factory.setValidationMode(ValidationMode.NONE);
factory.setPersistenceUnitName("testJPA");
return factory;
}
@Bean
public JpaTransactionManager transactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory((EntityManagerFactory) entityManagerFactory());
return transactionManager;
}
private Map<String, ?> createJpaProperties() {
Map<String, Object> propertyMap = new HashMap();
propertyMap.put("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
propertyMap.put("javax.persistence.schema-generation.database.action", "none");
propertyMap.put("hibernate.ejb.use_class_enhancer", true);
propertyMap.put("hibernate.hbm2ddl.auto", "create");
propertyMap.put("hibernate.show_sql", true);
return propertyMap;
}
}
UPDATE 2016-10 -04:我創建了Github repository which shows problem,在那裏您會看到該應用程序本身工作得很好(只需在index,jsp
文件中添加Actor
,但它不適用於測試)。附:我從Intellij IDEA(「運行」按鈕)將war:exploded
部署到Tomcat 8.0.24本地實例。
當你說,它運行良好,你用哪個應用服務器環context配置?它在共享庫中是否帶有hibernate? – AntJavaDev
@AntJavaDev我從Intellij IDEA(「運行」按鈕)部署'war:exploded'到Tomcat 8.0.24本地實例,我不確定,但我不認爲Tomcat在共享庫中有Hibernate,我甚至不認爲關於它早些時候:) –
好的,你的github的例子不是那麼有效,但無論如何,我設法在本地運行它與你應用的休眠版本(4.3.11.Final而不是5.你指的是你的帖子)它運行在Tomcat 8和JUnit runner上。我也得到它在hibernate 5.1.0.Final和5.2.0.Final下運行,在答案中提到。 – AntJavaDev