3 Replies Latest reply on Feb 9, 2017 2:37 AM by Martin Kouba

    Integration testing and JNDI resource injection

    Maxime Laval Newbie



      I want to test that a DAO/repository works properly with a test database (integration test). In the DAO I inject a JNDI datasource with the resource annotation:


      public class SomeDAO {
        @Resource(lookup = "java:comp/env/ds")
        private DataSource dataSource;


      I don't want to use any server during the tests, I just want to create a JNDI resource in some way and make Weld discover it and inject it.

      Currently I am trying to achieve this using the Simple-JNDI library: when I only test that the InitialContext contains the datasource it works, but when I test the DAO, Weld doesn't find the datasource. 

      How to create JNDI resources for Weld to inject, or how to make it work with Simple-JNDI?


      I run the tests with a class extending BlockJUnit4ClassRunner, WeldJUnit4Runner:




      Where WeldJUnit4Runner initialize Weld.

      Is there a way to add a datasource bean to the CDI, injectable with @Resource, before Weld creates an instance of the DAO?