Skip to content

Instantly share code, notes, and snippets.

@gunnarmorling
Created January 12, 2019 21:23
Show Gist options
  • Save gunnarmorling/87767faec3f359f8678ab69e700004d2 to your computer and use it in GitHub Desktop.
Save gunnarmorling/87767faec3f359f8678ab69e700004d2 to your computer and use it in GitHub Desktop.
package com.example.cditest;
import java.util.HashMap;
import java.util.Map;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.se.SeContainer;
import javax.enterprise.inject.se.SeContainerInitializer;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.BeanManager;
import javax.inject.Inject;
import org.jboss.weld.context.bound.BoundRequestContext;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
public class CdiTestBase {
private static SeContainer seContainer;
@Inject
BoundRequestContext requestContext;
private Map<String, Object> requestDataStore;
@BeforeClass
public static void startCdiContainer() {
seContainer = SeContainerInitializer.newInstance()
.addBeanClasses(EntityManagerFactoryProducer.class, EntityManagerProducer.class)
.initialize();
}
@AfterClass
public static void stopCdiContainer() {
seContainer.close();
}
@Before
public <T> void injectTestAndStartRequestScope() {
BeanManager beanManager = seContainer.getBeanManager();
AnnotatedType<T> at = (AnnotatedType<T>) beanManager.createAnnotatedType(getClass());
CreationalContext<T> ctx = beanManager.createCreationalContext(null);
beanManager.createInjectionTarget(at).inject((T)this, ctx);
requestDataStore = new HashMap<String, Object>();
requestContext.associate(requestDataStore);
requestContext.activate();
}
@After
public void stopRequestScope() {
try {
requestContext.invalidate();
requestContext.deactivate();
}
finally {
requestContext.dissociate(requestDataStore);
}
}
}
package com.example.cditest;
import java.util.Collections;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Disposes;
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.BeanManager;
import javax.inject.Inject;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
@ApplicationScoped
public class EntityManagerFactoryProducer {
@Inject
private BeanManager beanManager;
@Produces
@ApplicationScoped
public EntityManagerFactory produceEntityManagerFactory() {
return Persistence.createEntityManagerFactory(
"sinusTestPu",
Collections.singletonMap("javax.persistence.bean.manager", beanManager)
);
}
public void close(@Disposes EntityManagerFactory entityManagerFactory) {
entityManagerFactory.close();
}
}
package com.example.cditest;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Disposes;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
@ApplicationScoped
public class EntityManagerProducer {
@Inject
private EntityManagerFactory entityManagerFactory;
@Produces
@RequestScoped
public EntityManager produceEntityManager() {
return entityManagerFactory.createEntityManager();
}
public void close(@Disposes EntityManager entityManager) {
entityManager.close();
}
}
package com.example.cditest;
import java.util.UUID;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import org.junit.Before;
import org.junit.Test;
public class SomeTest extends CdiTestBase {
@Inject
private EntityManager entityManager;
@Inject
private SomeBean someBean;
@Test
public void test() {
//...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment