title: How to mock an injected bean in Mockito?
body: <p>This test works:</p>
  
  <pre><code>@RunWith(MockitoJUnitRunner.class)
  public class Test1 {
  
      @InjectMocks private MyBean bean;       
  
      @Test
      public void shouldWork() {      
          bean.test("ABC");       
      }
  }
  
  @Stateless
  public class MyBean {
  
      public String test(String s) {
          System.out.println("This is a test " + s);
          return s;
      }
  }
  </code></pre>
  
  <p>Now, I want to inject a new EJB MyBean2 in MyBean and retest. To do that, I mock MyBean2 in Test1 as well. This is the new test:</p>
  
  <pre><code>@RunWith(MockitoJUnitRunner.class)
  public class Test1 {
  
      @Mock MyBean2 bean2;        
      @InjectMocks MyBean bean;       
  
      @Test
      public void shouldWork() {
           bean.test("ABC");      
      }
  
  }
  
  
  @Stateless
  public class MyBean {
  
      @Inject
      MyBean2 bean2;
  
      public String test(String s) {
          return s + bean2.test2();
      }
  }
  
  
  @Stateless
  public class MyBean2 {
      public String test2() {
          return "DEF";
      }
  }
  </code></pre>
  
  <p>But when I run this I get <code>NullPointerException</code> when bean tries to invoke the bean2 method, probably because Mockito is not recognizing the injected bean as EJB.</p>
  
  <p>How to make this work?</p>