Hi Shri,
I'm glad you find CDI-Unit useful.
It may be a little difficult to mock the persistence context if
you are not using @Inject as @PersistenceContext is not part of
CDI. In
Seam
3 they worked around this and
they also
did something similar in Deltaspike, but if you are not
using something similar you may be out of luck.
If you are using @Inject to get your EntityManager then you can
use an H2 database to create an in memory database on the fly for
unit testing.
Below is an example utility class that I have used to test code
that relies on a database. It is using Seam3, but the same
principals can be applied elsewhere. When I want to use database
functionality I simply reference this class using
@AdditionalClasses on my test class.
Sorry I can't give you anything more specific as it very much
depends on your setup.
Bryn
@ApplicationScoped
@AdditionalClasses(value = { TransactionInterceptor.class,
TransactionExtension.class, TransactionScopeExtension.class,
org.jboss.seam.transaction.EntityTransaction.class,
SeSynchronizations.class,
HibernateManagedSessionExtension.class,
ManagedPersistenceContextExtension.class,
PersistenceContextsImpl.class, FlushModeManagerImpl.class })
public class H2DatabaseTest {
private Logger log =
LoggerFactory.getLogger(H2DatabaseTest.class);
private EntityManagerFactory entityManagerFactory;
@Inject
private BeanManager beanManager;
@ExtensionManaged
@Produces
@ApplicationScoped
public EntityManagerFactory getEntityManager() throws
IOException {
return entityManagerFactory;
}
@PostConstruct
public void postConstruct() throws IOException {
//Create our entity manager for the test
if (entityManagerFactory != null) {
throw new RuntimeException("Error");
}
Properties properties = new Properties();
//This properties file configures h2 for in memory
operation.
properties.load(ClassLoader.getSystemResourceAsStream("h2test.properties"));
//Scan the classpath for entities and configure hibernate
accordingly
Ejb3Configuration configuration = new Ejb3Configuration();
Set<Bean<?>> beans =
beanManager.getBeans(Object.class);
for (Bean<?> bean : beans) {
Class<?> beanClass = bean.getBeanClass();
if (beanClass.getAnnotation(Entity.class) != null ||
beanClass.getAnnotation(Embeddable.class) != null) {
log.debug("Registering entity {}", beanClass);
configuration.addAnnotatedClass(beanClass);
}
}
entityManagerFactory =
configuration.createEntityManagerFactory(properties);
}
@PreDestroy
public void preDestroy() throws IOException {
//H2 in memory databases are stored per JVM, so we have to
clear it out at the end of each test.
EntityManager entityManager =
entityManagerFactory.createEntityManager();
Query drop = entityManager.createNativeQuery("DROP ALL
OBJECTS");
EntityTransaction transaction =
entityManager.getTransaction();
transaction.begin();
entityManager.flush();
drop.executeUpdate();
transaction.commit();
entityManager.close();
entityManagerFactory.close();