2 Replies Latest reply on Apr 19, 2012 4:59 PM by Nathan Wallman

    Dependency Injection Not Working in a Rest Resource

    Nathan Wallman Newbie

      I have a couple of JUnit tests working with Dependency Injection already but for whatever reason I can't get this particular test to work.  The one thing that I see is different in this case is the Dependency Injection of the EJB is not happening inside of the unit test itself.  Instead the unit test is calling a Jersey Resource that is deployed along with the War.  My Jersey Resource has an EJB I'm trying to inject which isn't working.  Here is my code:



      public class SiteSettingsResourceTest {
                private static Integer TEST_SITE_ID = 1;
                public static Archive<?> createDeployment() {
                          return ShrinkWrap
                                              .create(WebArchive.class, "hapi-web.war")
                                              .addClasses(SiteManager.class, SiteManagerLocal.class, SiteDAO.class, SiteDAOLocal.class, SiteHibernateImpl.class, SiteJDBCImpl.class, AbstractResource.class,
                                                                  SiteSettingsResource.class).addAsResource("embedded/test-persistence.xml", "META-INF/persistence.xml")
                                              .addAsResource("embedded/test-queries.hbm.xml", "META-INF/test-queries.hbm.xml").addAsWebResource("embedded/test-web.xml", "WEB-INF/web.xml")
                                              .addAsWebResource(EmptyAsset.INSTANCE, "beans.xml");
                public void testGetSettingsHibernate() {
                          Client client = Client.create();
                          WebResource webResource = client.resource("" + TEST_SITE_ID);
                          ClientResponse response = webResource.accept("application/json").get(ClientResponse.class);
                          if (response.getStatus() != 200) {
                          } else {


      Then my JerseyResource looks like:




      public class SiteSettingsResource extends AbstractResource {
                private final Logger log = LoggerFactory.getLogger(this.getClass().getName());
                @EJB(mappedName = "java:global/hapi-web/SiteManager!com.noob.hapi.domain.mgr.SiteManagerLocal")
                private SiteManagerLocal siteManager;
                @Path("/hibernate/{siteid: [0-9]}")
                @Produces({ MediaType.TEXT_XML, MediaType.APPLICATION_JSON })
                public String getAllSettingsHibernate(@PathParam("siteid") int siteID) {
                          try {
                                    SiteSettings settings = siteManager.getSiteSettingsHibernate(siteID);
                                    return this.convertToJSON(settings);
                          } catch (SQLException | ConversionException e) {
                                    log.error("Error getting all settings for siteID: " + siteID);
                                    throw new InternalException(e.getMessage());



      I've tried various things but everytime I see my siteManager EJB is null.  I am assuming for whatever reason the Jersey resources are not being enriched for some reason.  I know my mappedName is correct because I pulled it right from the logs(INFO: EJB5181:Portable JNDI names for EJB SiteManager).  How can I ensure my Jersey Resources get enriched?  Thanks in advance.