title: Injection of @alternative bean with code, instead of beans.xml
body: <p>We're testing an EJB with EJBContainer in JUnit. Another EJB called by the tested bean is mocked by the test with an <code>@alternative</code> bean. This mock bean is configured as <code>&lt;alternative&gt;</code> in <code>beans.xml</code></p>
  
  <p>Everything works as like a charm, the EJB is tested and uses the mocked service.</p>
  
  <p>Question: Is it possible to do the same only with code, instead of using a beans.xml?</p>
  
  <p>Of course a <code>beans.xml</code> gives good flexibility, though sometimes one might want to have a different <code>@alternative</code> for the same bean and the possibility to select one specific for a single/other test in the same project. Perhaps another solution would be a specific <code>beans.xml</code> for certain tests (with the question how to select it)?</p>
  
  <p>Some of our test code (ExtensionMock is called by the tested EchoRemote implementation and part of the tests):</p>
  
  <pre><code>public class EchoTest {
  private EJBContainer ejbContainer;
  private Context ctx;
  
  
  @Before
  public void setUp() throws NamingException {
      ejbContainer = EJBContainer.createEJBContainer();
      ctx = ejbContainer.getContext();
  }
  
  @After
  public void tearDown() {
      ejbContainer.close();
  }
  
  @Test
  public void testFindAll() {
      try {
          EchoRemote userEJB = (EchoRemote) ctx.lookup("java:global/ssb-ejb/Echo!examples.ssb.EchoRemote");
          assertNotNull(userEJB);
          assertEquals("Hello World", userEJB.echo("Hello World"));
      } catch (NamingException e) {
          throw new AssertionError(e);
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <pre><code>&lt;beans&gt;
  
      &lt;alternatives&gt;
          &lt;class&gt;examples.ssb.EchoExtensionMock&lt;/class&gt;
      &lt;/alternatives&gt;
  
  &lt;/beans&gt;
  </code></pre>