1 Reply Latest reply: Aug 14, 2012 3:05 AM by ole . RSS

    Overwrite Annotation by using ejb-jar.xml not working

    ole . Newbie

      Hi,

       

      In my application I have an interface that is implemented by 2 classes (mock, productive). Due to the fact I need the mock also during my deployment for testing I have both implementations in my ejb package.

      When I try to inject one of these implementations I set the "beanName" property of @EJB to decide which implementation should be injected:

       

      @EJB(beanName = "MyInterfaceImpl1")
      MyInterface myInterface;
      

       

      If I want to use another implementation for this injection I'm using the ejb-jar.xml to overwrite the beanName:

       

      <?xml version="1.0" encoding="UTF-8"?>
      <ejb-jar version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd">
        <display-name>MyApp</display-name>
        <enterprise-beans>
      
          <session>
            <ejb-name>MyEJBean</ejb-name>
            <ejb-class>com.test.MyEJBean</ejb-class>
            <ejb-ref>
              <ejb-ref-name>com.test.MyEJBean/myInterface</ejb-ref-name>
              <ejb-ref-type>Session</ejb-ref-type>
              <ejb-link>MyInterfaceImpl2</ejb-link>
              <injection-target>
                <injection-target-class>com.test.MyEJBean</injection-target-class>
                <injection-target-name>myInterface</injection-target-name>
              </injection-target>
            </ejb-ref>
          </session>
        </enterprise-beans>
      </ejb-jar>
      

       

      This is working properly during a deployment on Glassfish 3.1. Instead of MyInterfaceImpl1 MyInterfaceImpl2 i used.

      Unfortunatly this is not working in Arquillian. I'm building an archive, adding the ejb-jar.xml and run the test, but the MyInterfaceImpl1 will be injected instead of MyInterfaceImpl2 wich results in a failing test.

      I build my arquillian archive like this:

       

        @Deployment
        public static WebArchive createDeployment() {
          WebArchive archive = ShrinkWrap
              .create(WebArchive.class, "test.war")
              .addClasses(MyEJBean.class, MyInterface.class,
                  MyInterfaceImpl2.class)
              .addAsWebInfResource("ejb-jar-test.xml", "ejb-jar.xml");
          System.out.println(archive.toString(true));
          return archive;
        }