在运行Junit测试时将@PersistenceContext更改为@PersistenceUnit

时间:2016-08-05 07:50:54

标签: spring hibernate junit

我现在正在使用@PersistenceContext并正常工作。但是,同样的应用程序也需要在Junit上下文中使用。当Junit测试用例正在运行时,我希望在@PersistenceContext类中将@PersistenceUnit更改为DaoImpl。怎么做?

在我的应用程序中使用以下版本

<springframework.version>4.2.5.RELEASE</springframework.version>
<hibernate.version>5.1.0.Final</hibernate.version>
<hibernate.validator.version>5.2.4.Final</hibernate.validator.version>
<mysql.connector.version>6.0.3</mysql.connector.version>
<junit.version>4.12</junit.version>

休眠配置是:

package com.rsa.springwebclasses.configuration.db;

import java.util.Properties;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
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 org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import ch.qos.logback.classic.Logger;

@Configuration
@EnableTransactionManagement
@PropertySources({
  @PropertySource("classpath:hibernate/hibernate.properties"),
  @PropertySource("classpath:mysql/mysql.properties")
})
public class HibernateConfiguration {
  Logger logger = (Logger) LoggerFactory.getLogger(HibernateConfiguration.class);

  @Autowired
  Environment env;

private Properties getHibernateProperties() {
    Properties hprop = new Properties();
    hprop.put("hibernate.dialect", env.getRequiredProperty("mysql.hibernate.dialect"));
    hprop.put("hibernate.show_sql", env.getRequiredProperty("hibernate.show_sql"));
    hprop.put("hibernate.format_sql", env.getRequiredProperty("hibernate.format_sql"));
    logger.info("Hibernate properties: " + hprop.toString());
    return hprop;
}

private Properties getMysqlProperties() {
    Properties mySqlProp = new Properties();
    mySqlProp.put("jdbc.driverClassName", env.getRequiredProperty("mysql.jdbc.driverClassName"));
    mySqlProp.put("jdbc.url", env.getRequiredProperty("test.mysql.jdbc.url"));
    mySqlProp.put("jdbc.username", env.getRequiredProperty("test.mysql.jdbc.username"));
    mySqlProp.put("jdbc.password", env.getRequiredProperty("test.mysql.jdbc.password"));
    logger.info("MySQL properties: " + mySqlProp.toString());
    return mySqlProp;
}

@Bean
public DataSource getDataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    Properties dbProp = this.getMysqlProperties();
    dataSource.setDriverClassName(dbProp.getProperty("jdbc.driverClassName"));
    dataSource.setUrl(dbProp.getProperty("jdbc.url"));
    dataSource.setUsername(dbProp.getProperty("jdbc.username"));
    dataSource.setPassword(dbProp.getProperty("jdbc.password"));
    logger.info("DataSource object is created.");
    return dataSource;
}

@Bean
public PlatformTransactionManager getTransactionManager(EntityManagerFactory emf) {
    JpaTransactionManager jtm = new JpaTransactionManager();
    jtm.setEntityManagerFactory(emf);
    logger.info("PlatformTransactionManager is initialized with JpaTransactionManager");
    return jtm;
}

@Bean
public LocalContainerEntityManagerFactoryBean getEntityManagerFactory() {
    LocalContainerEntityManagerFactoryBean emfb = new LocalContainerEntityManagerFactoryBean();
    emfb.setDataSource(getDataSource());
    emfb.setPackagesToScan(new String[] {"com.rsa.springwebclasses.model"});
    JpaVendorAdapter vendor = new HibernateJpaVendorAdapter();
    emfb.setJpaVendorAdapter(vendor);
    emfb.setJpaProperties(getHibernateProperties());
    logger.info("LocalContainerEntityManagerFactoryBean created: PersistentUnit Name: " + emfb.getJpaVendorAdapter().toString());
    return emfb;
}

}

我的DaoImpl课程是:

package com.rsa.springwebclasses.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.rsa.springwebclasses.dao.UserDao;
import com.rsa.springwebclasses.model.User;

import ch.qos.logback.classic.Logger;

@Repository("userDao")
public class UserDaoImpl implements UserDao {
   Logger logger = (Logger) LoggerFactory.getLogger(UserDaoImpl.class);

  **@PersistenceContext**
  EntityManager em;

public List<User> findAllUsers() {
    logger.debug("In findAllUsers method.");
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<User> criteria = cb.createQuery(User.class);
    Root<User> user = criteria.from(User.class);
    criteria.select(user).orderBy(cb.asc(user.get("username")));
    logger.debug("Exit findAllUsers method");
    return em.createQuery(criteria).getResultList();
  }
}

注入EntityManager时,@PersistenceContext是硬编码的。但是,同样的DaoImpl类也需要用于Junit测试用例。使用Junit,我更喜欢将事务类型用作RESOUCE_LOCAL,这仅适用于@PersistenceUnit。是否可以在DaoImpl类中使用不同的注释,并在应用程序加载或已经运行时分配@PersistenceContext@PersistenceUnit

0 个答案:

没有答案