title: JEE: Replace an injected attribute with a mock for unit testing, outside of the container
body: <p>I have an EJB which I want to test.  I'm coding for JBoss EAP 6.4, but I want to unit test.  The EJB (SendMessageTimer) inherits from another class (ResettableTimer), which has a TimerService (theoretically provided by EAP) injected with @EJB (also tried @Resource).
  I found the following: <a href="https://stackoverflow.com/questions/5777782/mocking-ejb-injection-in-tests">Mocking EJB injection in tests</a> which is roughly what I want to do, but easygloss appears to be dead - no downloads that I can see, hasn't been updated in 6 years.  I started with this, which does something similar but with datasources, so it seems like it should be possible: <a href="https://blogs.oracle.com/randystuph/entry/injecting_jndi_datasources_for_junit" rel="nofollow noreferrer">https://blogs.oracle.com/randystuph/entry/injecting_jndi_datasources_for_junit</a> </p>
  
  <pre><code>public abstract class parentTimer {
  
    @EJB
    private static TimerService timerService;
  
    Timer resettableTimer = null;
  
    public void reset(){
         //Nullpointer here!
         resettableTimer = timerService.createTimer(timeout, getName());
    }
  
    @Timeout
    public abstract void doTimerActions() throws Exception;
  }
  
  @EJB
  public class SendTimer extends ResetableTimer {
     public SendTimer() {}
  
     @Override
     @Timeout
     public void doTimerActions() throws IOException, JMSException { 
         this.reset()
     }
  }
  
  public class SendTimerTest {
  
      private static InitialContext ic;    
  
      @BeforeClass
      public static void setUpClass() throws Exception {
  
          ic = createInitialContext();
          makeAllSubcontexts(ic, "java:/javax/ejb");
  
          TimerService mockTimerService = Mockito.mock(TimerService.class);
          Timer mockTimer = Mockito.mock(Timer.class);
  
          Mockito.when(mockTimerService.createTimer(Mockito.anyLong(), Mockito.any(String.class))).thenReturn(mockTimer);
  
          ic.bind("java:/javax/ejb/TimerService", mockTimerService);
      }
  
      private static InitialContext createInitialContext() throws NamingException {
  
          if (ic != null) {
              return ic;
          }
  
          // Create initial context
          Hashtable ht = new Hashtable();
          ht.put(InitialContext.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
          ht.put(InitialContext.URL_PKG_PREFIXES, "org.apache.naming");
          ic = new InitialContext(ht);
          return ic;
      }
  
      @Test
      public void testDoAcTimerActions() {
          TimerService service = (TimerService) ic.lookup("java:/javax/ejb/TimerService");
          //This context is created successfully because the assert here passes
          assertNotNull(service);
  
          makeAllSubcontexts(ic, "java:/my/timers/package");
  
          SendTimer sendTimer = new SendTimer();
          sendTimer.setTimerService(service);
          sendTimer.reset();
          ic.bind("java:/my/timers/package/SendTimer", sendTimer);
          SendTimer timer = (SendTimer) ic.lookup("java:/my/timers/package/SendTimer");
  
      assertNotNull(timer);
  
      timer.doTimerActions();  //Nullpointer from parentTimer
  
  }
  </code></pre>
  
  <p>I get a nullpointer at the TimerService call.  I assume this is because the sendTimer is created stand-alone rather than being initialised by the ic context.  The other thing is possibly I'm using the wrong properties in the hashtable for the initial context.  There seem to be many different ones available, but I'm not exactly sure of the differences.  What I want to know, is is it possible to create the sendTimer from the context (where it would presumably have the TimerService injected) instead of binding a ready-created one; does it even make sense to do that?  Should I leave this approach and use a testing framework instead, if so any recommendations?
  Thanks in advance for any help you can give me.</p>