Spring 如何在测试中重新配置LocalContainerEntityManagerFactoryBean
如果我有一个编程管理的LocalContainerEntityManagerFactoryBean,比如@Bean注释方法:Spring 如何在测试中重新配置LocalContainerEntityManagerFactoryBean,spring,spring-boot,spring-boot-test,Spring,Spring Boot,Spring Boot Test,如果我有一个编程管理的LocalContainerEntityManagerFactoryBean,比如@Bean注释方法: @Configuration @EnableJpaRepositories( entityManagerFactoryRef = "myEntityManager", transactionManagerRef = "myTransactionManager", basePackages = {"com.mycompany.my.repository"} )
@Configuration
@EnableJpaRepositories(
entityManagerFactoryRef = "myEntityManager",
transactionManagerRef = "myTransactionManager",
basePackages = {"com.mycompany.my.repository"}
)
public class MyDbConfig {
@Bean
public LocalContainerEntityManagerFactoryBean mcEntityManager() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
//some production config here
return em;
}
}
我如何添加这个仅用于集成测试的小JPA属性,最好是通过下面的监听器(在测试类路径中)来添加
public类之后beanscreatedListener{
@自动连线
私有LocalContainerEntityManagerFactoryBean em
@施工后
私有void重新配置(){
HashMap属性=em.getJpaPropertyMap()
properties.put(“hibernate.hbm2ddl.auto”、“create”);
em.setJpaPropertyMap(属性);
}
}
目前,我正在使用另一个@Bean注释方法在嵌套的@TestConfiguration中生成LocalContainerEntityManagerFactoryBean,但这意味着我需要复制该Bean的所有其他创建逻辑,这当然是纯粹的邪恶。快速而简单的解决方案是在mcEntityManager()中创建属性映射这样的方法
@Configuration
@EnableJpaRepositories(
entityManagerFactoryRef = "myEntityManager",
transactionManagerRef = "myTransactionManager",
basePackages = {"com.mycompany.my.repository"}
)
public class MyDbConfig {
@Bean
public LocalContainerEntityManagerFactoryBean mcEntityManager() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
HashMap<String, Object> properties = em.getJpaPropertyMap()
properties.put("hibernate.hbm2ddl.auto", "create");
em.setJpaPropertyMap(properties);
return em;
}
}
快速而简单的解决方案是在mcEntityManager()方法中创建属性映射,如下所示
@Configuration
@EnableJpaRepositories(
entityManagerFactoryRef = "myEntityManager",
transactionManagerRef = "myTransactionManager",
basePackages = {"com.mycompany.my.repository"}
)
public class MyDbConfig {
@Bean
public LocalContainerEntityManagerFactoryBean mcEntityManager() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
HashMap<String, Object> properties = em.getJpaPropertyMap()
properties.put("hibernate.hbm2ddl.auto", "create");
em.setJpaPropertyMap(properties);
return em;
}
}
您可能需要注入jpaPropertyMap。否则,您将在编译时得到方法签名冲突,您可能需要注入jpaPropertyMap。否则,您将在编译时得到方法签名冲突
@Bean
@Profile("dev)
public Map<String,Object> getMyJpaPropertyMap() {
HashMap<String, Object> properties = new HashMap<>();
properties.put("hibernate.hbm2ddl.auto", "create");
properties.put("hibernate.temp.use_jdbc_metadata_defaults",false);
properties.put("hibernate.jdbc.lob.non_contextual_creation",true);
}
@Bean
@Profile("prod)
public Map<String,Object> getMyJpaPropertyMap() {
HashMap<String, Object> properties = new HashMap<>();
properties.put("hibernate.hbm2ddl.auto", "update");
properties.put("hibernate.temp.use_jdbc_metadata_defaults",true);
}
@Bean
public LocalContainerEntityManagerFactoryBean mcEntityManager() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setJpaPropertyMap(getMyJpaPropertyMap());
return em;
}