2 Replies Latest reply on Apr 12, 2018 8:13 AM by Mauro Reinehr

    Problems testing application with (Junit 5 - Weld 3 - CDI)

    Mauro Reinehr Newbie

      Hello!
      I have the following classes:

      public interface SessionFactoryTemplate {
      
           public Session getCurrentSession();
      
           public Session openSession();
      
           public StatelessSession openStatelessSession();
      
      }

       

      @Singleton
      public class SessionFactoryBuilder {
           private SessionFactory sessionFactory;
      
           @PostConstruct
           void buildSessionFactory() {
                final StandardServiceRegistry standardServiceRegistry = new StandardServiceRegistryBuilder().configure().build();
                try {
                     Metadata metadata = new MetadataSources(standardServiceRegistry).buildMetadata();
                     sessionFactory = metadata.buildSessionFactory();
                } catch (Exception e) {
                     StandardServiceRegistryBuilder.destroy(standardServiceRegistry);
                     e.printStackTrace();
                     throw new ExceptionInInitializerError(e);
                }
           }
      
           @Produces
           public SessionFactory getSessionFactory() {
                return sessionFactory;
           }
      
           @PreDestroy
           void destroySessionFactory() {
                sessionFactory.close();
           }
      }


      and finally the class that will be injected into the test:

      @RequestScoped
      public class AppSessionFactory implements SessionFactoryTemplate {
      
           @Inject
           private SessionFactory sessionFactory;
      
           private Session currentSession;
      
           @Override
           public Session openSession() {
                return sessionFactory.openSession();
           }
      
           @Override
           public Session getCurrentSession() {
                if (currentSession == null) {
                     currentSession = sessionFactory.openSession();
                }
                     return currentSession;
             }
      
           @Override
           public StatelessSession openStatelessSession() {
                return sessionFactory.openStatelessSession();
           }
      
           @PreDestroy
           private void closeSession() {
                if (currentSession != null && currentSession.isOpen()) {
                     currentSession.close();
                }
            }
      
      }


      And here's the class to run the tests:

      @EnableWeld
      public class TesteWeld {
      
           @Inject
           private AppSessionFactory sf;
      
           @WeldSetup
           public WeldInitiator weld = WeldInitiator.from(Usuario.class).activate(RequestScoped.class, SessionScoped.class).build();
      
      
           @Test
           public void listarUsuarios() {
                List usuarios = new ArrayList<>();
                CriteriaBuilder builder = sf.getCurrentSession().getCriteriaBuilder();
                CriteriaQuery criteria = builder.createQuery(Usuario.class);
                Root root = criteria.from(Usuario.class);
                criteria.select(root);
                criteria.orderBy(builder.asc(root.get(Usuario_.nomeUsuario)));
                TypedQuery q = sf.getCurrentSession().createQuery(criteria);
                usuarios = q.getResultList();
                for (Usuario u : usuarios) {
                     System.out.println(u.getNomeUsuario());
                }
           }
      }

       

      When you try to run the test with junit, you are returning the following error:

      org.jboss.weld.exceptions.IllegalArgumentException: WELD-001408: Unsatisfied dependencies for type AppSessionFactory with qualifiers @Default

      would anyone have a hint of what I'm doing wrong?

      Thank you!