I have generic abstract class AbstractBaseEntityGenericDao which contains #Autowired field. It worked perfectly until I had to write a unit test for it, to not duplicate the same code inside all tests for classes which extends it. And now I'm thinking...Is it possible to write a unit/integration test for such class?
#Repository
#Transactional
public abstract class AbstractBaseEntityGenericDao<T extends BaseEntity> {
private Class<T> classInstance;
private SessionFactory sessionFactory;
#Autowired
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public final void setClassInstance(Class<T> clasInstance) {
this.classInstance = clasInstance;
}
public void create(#NonNull T entity) {
Session session = sessionFactory.getCurrentSession();
session.save(entity);
}
public Optional<T> find(long id) {
Session session = sessionFactory.getCurrentSession();
return Optional.ofNullable(session.get(classInstance, id));
}
public void update(#NonNull T entity) {
Session session = sessionFactory.getCurrentSession();
session.saveOrUpdate(entity);
}
public void remove(#NonNull Long id) throws EntityNotFoundException {
Session session = sessionFactory.getCurrentSession();
session.remove(session.load(classInstance, id));
}
public void remove(#NonNull T entity) {
Session session = sessionFactory.getCurrentSession();
session.remove(entity);
}
}
The reason this is difficult is because generally you should not be doing this. The abstract class should have no knowledge of how its child creates SessionFactory. so instead it should look something like:
#Repository
#Transactional
public abstract class AbstractBaseEntityGenericDao<T extends BaseEntity> {
...
protected SessionFactory sessionFactory;
...
}
Now you CANNOT directly unit test a abstract class as it can not be instantiated. you can however stub it out in a unit test, and test that stub. The stub in turn will have a constructor for the protected field which we can mock out in the unit test. In the end it would look like:
public class AbstractBaseEntityGenericDaoTest {
private class AbstractClassStub extends AbstractBaseEntityGenericDao {
public AbstractClassStub(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
#Override
public void create(BaseEntity entity) {
super.create(entity);
}
#Override
public Optional find(long id) {
return super.find(id);
}
#Override
public void update(BaseEntity entity) {
super.update(entity);
}
#Override
public void remove(#NonNull Long id) throws EntityNotFoundException {
super.remove(id);
}
#Override
public void remove(BaseEntity entity) {
super.remove(entity);
}
}
#Mock
SessionFactory sessionFactory;
private AbstractClassStub abstractClassStub;
#Before
public void before() {
sessionFactory = mock(SessionFactory.class);
abstractClassStub = new AbstractClassStub(sessionFactory);
}
#Test
public void testWhatever() {
abstractClassStub.find(1); //or whatever
}
}
Related
how can i inject EntityManager(jpa) with Mockito?
I wanna bind Mockito.spy(UserService.class) to guice injector.
but UserService.class has EntityManager for query execution.
When installing 'JunitServiceModule' in guice injector, EntityManager is not found.
See below for error details
com.google.inject.CreationException: Unable to create injector, see the following errors:
1) Error in custom provider, java.lang.NullPointerException
while locating com.google.inject.persist.jpa.JpaPersistService
while locating javax.persistence.EntityManager
My code is below.
(The test code just made the error situation similar.)
(actually 'EntityManager' is located in UserRepository... It's just example!)
(#Transactional is belong to guice)
public class UserServiceTest {
#Inject
private UserService userService;
#Before
public void setUp() {
Injector injector = new TestBuilder().init();
injector.initMembers(this);
Mockito.doReturn(10).when(userService).getEntityCount(UserEntity.class);
}
#Test
public void test() {
assertEquals(10, userService.getEntityCount(UserEntity.class));
}
}
public class TestBuilder {
public TestBuilder() {
}
public Injector init() {
Injector injector = Guice.createInjector(
new TestDBInjectModule("test"),
new JunitServiceModule()
);
}
}
public class TestDBInjectModule extends AbstractModule {
private String unitName;
public TestDBInjectModule(String unitName) {
this.unitName = unitName;
}
#Override
protected void configure() {
install(new JpaPersistModule(unitName));
bind(JpaInitializer.class).asEagerSingleton();
}
#Singleton
private static class JpaInitializer {
#Inject
public JpaInitializer(final PersistService persistService) {
persistService.start();
}
}
}
public class JunitServiceModule extends AbstractModule {
#Override
protected void configure() {
bind(UserService.class).toInstance(Mockito.spy(UserService.class));
}
}
public class UserService {
#Inject
private EntityManager entityManager;
public UserService {} // <-- throw NullPointerException!!! since EntityManager
#Transactional
public void addUser(User user) {
return entityManager.persist(user);
}
public Number getCount() {
return entityManager.createQuery("select count(*) from user", Number.class).getSingleResult();
}
}
When I execute from DAO layer(ProjectDAOImpl), I'am able to see the results from database.
However when I execute from results from Service layer, I'am getting null pointer exceptions.
java.lang.NullPointerException
ProjectServiceImpl.fetchProjects(ProjectServiceImpl.java:25)
ProjectServiceImpl.main(ProjectServiceImpl.java:36)
How can I resolve this issue?
DAO Layer Interface and Class
#Remote
public interface ProjectDAO {
List<Project> fetchProjectDetail();
}
#Stateless
public class ProjectDAOImpl implements ProjectDAO {
private EntityManagerFactory entityManagerFactory =
Persistence.createEntityManagerFactory("projects");
public ProjectDAOImpl() {
}
public List<Project> fetchProjectDetail() {
List<Project> listProject = new ArrayList<Project>();
listProject =
getEntityManager().createNamedQuery("Project.findProjects").getResultList();
return listProject;
}
private EntityManager getEntityManager() {
return entityManagerFactory.createEntityManager();
}
}
Service Layer Interface and Class
#Remote
public interface ProjectService {
List<Project> fetchProjectDetail();
}
#Stateless
public class ProjectServiceImpl implements ProjectService {
private ProjectDAO projectDAO;
public ProjectServiceImpl() {
}
#EJB
public void setProjectDAO(ProjectDAO projectDAO) {
this.projectDAO = projectDAO;
}
#GET
#Path("/projects")
#Produces(MediaType.APPLICATION_JSON)
public List<Project> fetchProjects() {
return getProjectDAO().fetchProjectDetail();
}
public ProjectDAO getProjectDAO() {
return projectDAO;
}
public static void main(String [] args) {
ProjectServiceImpl projectServiceImpl = new ProjectServiceImpl();
projectServiceImpl.fetchProjects();
}
}
I have an abstract DAO:
public abstract class AbstractJpaDAO<T extends Serializable> implements I_AbstractJpaDAO<T> {
private Class<T> clazz;
#PersistenceContext
protected EntityManager entityManager;
public final void setClazz(final Class<T> clazzToSet) {
this.clazz = clazzToSet;
}
#Override
public T findOne(final long id) {
return entityManager.find(clazz, id);
}
#Override
public List<T> findAll() {
return entityManager.createQuery("from " + clazz.getName()).getResultList();
}
#Override
public void create(final T entity) {
entityManager.persist(entity);
}
#Override
public T update(final T entity) {
return entityManager.merge(entity);
}
#Override
public void delete(final T entity) {
entityManager.remove(entity);
}
#Override
public void deleteById(final long entityId) {
final T entity = findOne(entityId);
delete(entity);
}
}
I then extend this DAO in each DAO implementation (code not included) but header something like:
public class UserDAOImpl extends AbstractJpaDAO <User> implements UserDAO {
.....
With each entity, I work with a base interface type, for this example,let's call it UserDAO, and have an associated implementation, let's call it, UserDAOIMPL. To avoid having to define the same methods each Interface to each DAO. As in this longwinded example, i.e :
public interface UserDAO {
User findOne(long id);
List<User> findAll();
void create(User user);
User update(User user);
void delete(User user);
void deleteById(long userID);
User findUserByUserName(String name);
EntityManager returnEntityManager();
}
I would like to instead create a base interface, that all DAOs can extend.
public interface I_AbstractJpaDAO<T> {
.....
}
and then use this in each DAO interface.
public interface userDAO extends I_AbstractJpaDAO<T> { .....
However, I'm getting problems with "both methods have erasure, but neither overrides the other". Something to do with the Serialization thing I suspect. Can anyone help me please?
It means that, your base interface and abstract interface have the methods with the same signature and different return type.
Try this:
public interface userDAO extends I_AbstractJpaDAO<User> { .....
So I have such classes
BaseDao
public interface BaseDao<T> {
T save(T entity);
}
UserDao
public interface UserDao extends BaseDao<User> {
User getUserByUserName(String name);
}
GenericDao
public abstract class GenericDao implements BaseDao {
#Autowired
protected SessionFactory sessionFactory;
private Class<?> getEntityClass() {
return ((Class) ((ParameterizedType) getClass().getGenericSuperclass())
.getActualTypeArguments()[0]);
}
public T save(T entity) {
Integer id = (Integer) sessionFactory.getCurrentSession().save(entity);
return (T) sessionFactory.getCurrentSession().get(getEntityClass(), id);
}
}
UserDaoImpl
#Repository
public class PgUserDaoImpl extends GenericDao<User> implements UserDao {
public User getUserByUserName(String name) {
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(
User.class);
criteria.add(Restrictions.eq("name", name));
return (User) criteria.uniqueResult();
}
}
GenericService
public class GenericService<T extends GenericDao<E>, E> {
protected T dao;
public GenericService(T dao) {
setDao(dao);
}
#Transactional
public E save(E entity) {
return dao.save(entity);
}
}
UserServiceImpl
#Service("userServiceImpl")
public class UserServiceImpl extends GenericService<PgUserDaoImpl, User>
implements UserService {
#Autowired
public UserServiceImpl(PgUserDaoImpl dao) {
super(dao);
}
#Autowired
private UserAssebmler assebmler;
#Transactional
#Override
public UserDetails loadUserByUsername(String name)
throws UsernameNotFoundException {
.....
}
}
So I write test contoller to save user, but I always get Exception
org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
The contoller
#
Controller
#RequestMapping(value = "/test")
public class TestController {
#Autowired
private UserServiceImpl userService;
#RequestMapping(value = "save", method = RequestMethod.GET)
public String test() {
User user = new User();
user.setName("admin");
user.setPassword("21232f297a57a5a743894a0e4a801fc3");
user.setRole(UserRole.ADMIN);
userService.save(user);
return "home";
}
}
Anybody know hot to fix that? Thanks :)
I've solved my question by extending GenericDao class from HibernateDaoSupport class.
I want to use container managed transaction in one class and share it with its subclasses.
Here is my abstract class :
#TransactionManagement(TransactionManagementType.CONTAINER)
public abstract class AbstractDAO {
#PersistenceContext(unitName = "myDS")
protected EntityManager em;
#Resource
protected SessionContext context;
protected Logger log;
public AbstractDAO() {
log = LoggerFactory.getLogger(this.getClass());
}
}
One of its child :
#Stateless
#TransactionManagement(TransactionManagementType.CONTAINER)
public class OrdreDAO extends AbstractDAO {
public OrdreDAO() {
}
#TransactionAttribute(TransactionAttributeType.REQUIRED)
public void persist(Ordre o) {
em.persist(o);// NPE here ... no EntityManager injected !
}
#SuppressWarnings("unchecked")
public List<Ordre> findAll() {
Query q = em.createQuery("from Ordre");
return q.getResultList();
}
}
On top of this child , OrdreService :
public class OrdreService {
private OrdreDAO dao;
public OrdreService() {
dao=new OrdreDAO();
}
public void persist(Ordre o) {
System.out.println("Service::persist ??");
dao.persist(o);
}
public List<Ordre> getOrdres() {
return dao.findAll();
}
public Ordre getOrdre(String id) {
return dao.findByPK(id);
}
public Ordre merge(Ordre o) {
return dao.merge(o);
}
}
A servlet using it :
public class creerOrdre extends HttpServlet {
private static final long serialVersionUID = 1L;
private OrdreService os;
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
try {
System.out.println("création ordre");
Ordre o = new Ordre();
o.setDate(req.getParameter("date"));
o.setMotif(req.getParameter("motif"));
System.out.println("Ordre: " + o.getDate() + " " + o.getMotif());
OrdreService os = new OrdreService()
if (os!=null) {
System.out.println("NON null !");
}
os.persist(o);
resp.sendRedirect("visualiser.jsp");
} catch (ParseException pe) {
throw new ServletException(pe);
}
}
}
I get a NPE when I try to persist an Ordre
What am I missing ?
JDK 6
JBoss 5.1.0.GA
JPA 1
Your OrdreService POJO class is not managed by the web container, and also it seems to me that it's a useless additional layer. I would use only the OrdreDAO.
Anyway if you want to keep both classes, make OrdreService a Stateless EJB. Inject in OrdreService the OrdreDAO instance using:
#EJB private OrdreDAO dao;
Finally, inject OrdreService in your servlet using:
#EJB private OrdreService os;
This should work.