Created
April 12, 2013 08:53
-
-
Save zbigniewTomczak/5370616 to your computer and use it in GitHub Desktop.
Test stateless bean with @Inject managed fields as a pojo with mocks from mockito, persistence provider with manual transaction management. Doesn't require any changes in tested bean (no setters nor constructors setters).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tomczak.test; | |
import java.lang.reflect.Field; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.logging.Logger; | |
import javax.enterprise.event.Event; | |
import javax.inject.Inject; | |
import javax.persistence.EntityManager; | |
import javax.persistence.EntityManagerFactory; | |
import javax.persistence.Persistence; | |
import org.jboss.tools.example.richfaces.model.Member; | |
import org.jboss.tools.example.richfaces.service.MemberRegistration; | |
import org.junit.After; | |
import org.junit.Before; | |
import org.junit.BeforeClass; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.mockito.Mock; | |
import org.mockito.runners.MockitoJUnitRunner; | |
@RunWith(MockitoJUnitRunner.class) | |
public class MemberRegistrationTest { | |
private static EntityManagerFactory emf; | |
private EntityManager em; | |
@Mock | |
private Logger logger; | |
@Mock | |
private Event event; | |
@Test | |
public void test() throws Exception { | |
Member m = new Member(); | |
m.setEmail("[email protected]"); | |
m.setName("saadfadfasdf"); | |
m.setPhoneNumber("23423432443"); | |
MemberRegistration mr = new MemberRegistration(); | |
inject(mr, em); | |
inject(mr, logger); | |
inject(mr, event); | |
mr.register(m); | |
} | |
private void inject(Object obj, Object val) throws IllegalArgumentException, IllegalAccessException { | |
Class<?> klass = obj.getClass(); | |
while (klass != Object.class) { // need to iterated thought hierarchy in order to retrieve methods from above the current instance | |
// iterate though the list of methods declared in the class represented by klass variable, and add those annotated with the specified annotation | |
final List<Field> allFields = new ArrayList<Field>(Arrays.asList(klass.getDeclaredFields())); | |
for (final Field field : allFields) { | |
if (field.getType().isInstance(val) && field.isAnnotationPresent(Inject.class)) { | |
field.setAccessible(true); | |
field.set(obj, val); | |
} | |
} | |
// move to the upper class in the hierarchy in search for more methods | |
klass = klass.getSuperclass(); | |
} | |
} | |
@BeforeClass | |
public static void init() { | |
emf = Persistence.createEntityManagerFactory("primary"); | |
} | |
@Before | |
public void before() { | |
em = emf.createEntityManager(); | |
em.getTransaction().begin(); | |
} | |
@After | |
public void after() { | |
if (em.isOpen()) { | |
if (em.getTransaction().isActive()) { | |
em.getTransaction().commit(); | |
} | |
em.close(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment