Aissasa
Aissasa

Reputation: 231

Using JUnit to test my dao layer : Spring and hibernate with JavaConfig

I'm working on a spring web application using hibernate and spring security.

After implementing the DAO (using generic dao pattern) and making a CustomUserDetailService for and CustomUserDetails for spring security, it seems I can't login (I was using in memory ids and it was working) so I thought that it must be a problem in my DAO.

1) I'm using JavaConfig for my configuration and this initializer :

public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] { AppConfig.class,SecurityConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return null;
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }

}

I've never used JUnit testing before, and all the examples I found use xml config so I need some help on that, an example would be great.

2) Since I couldn't use JUnit I tried to get an idea what the problem was so I added a controller method where I get some users and show them in a page, but my GlobalException Handler intercepted that and redirected me to the error page with this exception :

org.springframework.orm.hibernate4.HibernateSystemException: No Session found for current thread; nested exception is org.hibernate.HibernateException: No Session found for current thread

Here's my GenericDaoImpl:

public class GenericDaoImpl<T,ID extends Serializable> implements GenericDao<T,ID> {


    private Class<T> persistentClass;

    @SuppressWarnings("unchecked")
    public GenericDaoImpl(Class<T> persistentClass) {
        this.persistentClass = persistentClass;
    }

    @Autowired
    private SessionFactory sessionFactory;

    public Session getSession()
    {
        return sessionFactory.getCurrentSession();
    }


    @Override
    public boolean create(T t) {

        if (t== null)  return false;
        getSession().saveOrUpdate(t);
        return true;
    }

    @SuppressWarnings("unchecked")
    @Override
    public T getById(ID id) {

         return (T) getSession().get(persistentClass,id);
    }

    @Override
    public boolean update(T t) {

        if (t==null) return false;
        getSession().update(t);
        return true;
    }

    @Override
    public boolean delete(T t) {

        if (t==null) return false;
        getSession().delete(t);
        return true;
    }

    @Override
    public boolean deleteById(ID id) {
        T t =getById(id);
        if (t==null) return false;
        getSession().delete(t);
        return true;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<T> findByOneCriteria(Criterion criterion) {
        Criteria criteria = getSession().createCriteria(persistentClass);
        criteria.add(criterion);
        return (List<T>)criteria.list();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<T> findByCriterias(ArrayList<Criterion> criterions) {
        Criteria criteria = getSession().createCriteria(persistentClass);
        for (Criterion c : criterions)
        criteria.add(c);
        return (List<T>)criteria.list();
    }
}

And my PersonnelDaoImpl:

@Repository
public class PersonnelDaoImpl extends GenericDaoImpl<Personnel,Integer> implements PersonnelDao {

    public PersonnelDaoImpl(){
        super(Personnel.class);
    }

    @Override
    public Personnel findByUsername(String username) {
       List<Personnel> result = findByOneCriteria(Restrictions.like("username",username));
       return result.get(0);
    }

    @Override
    public List<Personnel> findByRole(int role) {
        return findByOneCriteria(Restrictions.eq("role",role));
    }
}

I don't get what's wrong.

Upvotes: 1

Views: 1404

Answers (2)

Vinay Veluri
Vinay Veluri

Reputation: 6865

Creating configuration for the test case using Java,

Use these annotations on your test class.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {Config.class})
public class ServiceTest{

}

and the Config.class would look like,

@Configuration
public class Config {
}

Define the beans in the Config class and execute the test case.

2) Its crystal clear that No session indicates that there is not TransactionManager defined in the layer that calls your DAL.

Try to use @Transactional which does that for you.

Note: Please make sure that @Transactional allows value attribute which is defaulted to transactionManager

Upvotes: 1

Aissasa
Aissasa

Reputation: 231

Concerning the second part 2), i found the solution, i had to add @Transactional to my service, this post helped me

Upvotes: 0

Related Questions