7 Replies Latest reply on Jun 9, 2010 10:39 AM by Aslak Knutsen

    Problem with @EJB injection

    Stefan Rasmusson Newbie

      Hi, first i just want to say I really like this project and I think it will become verry populare.

      Now for the problems.

       

      I'm trying to EJB into a test this works fine until the EJB implements a local interface. Then verry stange problems. All my tests pass, even if they shouldn't.

       

       

      The test

      import javax.ejb.EJB;
      import org.jboss.arquillian.api.Deployment;
      import org.jboss.arquillian.testng.Arquillian;
      import org.jboss.shrinkwrap.api.ArchivePaths;
      import org.jboss.shrinkwrap.api.ShrinkWrap;
      import org.jboss.shrinkwrap.api.spec.JavaArchive;
      import org.jboss.shrinkwrap.impl.base.asset.ByteArrayAsset;
      import org.testng.Assert;
      import org.testng.annotations.Test;
      public class TemperatureConverterTest extends Arquillian {
         @EJB
         private TemperatureConverter converter;
         @Test
         public void testConvertToCelsius() {
           Assert.fail();
           Assert.assertEquals(converter.convertToCelsius(32d), 0d);
           Assert.assertEquals(converter.convertToCelsius(212d), 100d);
         }
       
       
         @Test
         public void testConvertToFarenheit() {
            Assert.assertEquals(converter.convertToFarenheit(0d), 32d);
            Assert.assertEquals(converter.convertToFarenheit(100d), 212d);
         }
         @Deployment
         public static JavaArchive createTestArchive() {
         JavaArchive ja = ShrinkWrap.create("test6.jar", JavaArchive.class)
         .addClass(TemperatureConverter.class)
         .addClass(TemperatureConverterImpl.class)
         .addManifestResource(new ByteArrayAsset(new byte[0]), ArchivePaths.create("beans.xml"));
         System.out.println();
         System.out.println("Printing classes");
         System.out.println();
         ja.toString(Formatters.SIMPLE);
         System.out.println();
      return ja;
        }
      }
      
      
      

       

      The EJB

       

      import javax.ejb.Local;
      import javax.ejb.Stateless;
      
      @Local(TemperatureConverter.class)
      @Stateless
      public class TemperatureConverterImpl implements TemperatureConverter {
      
           public double convertToCelsius(double f) {
                return ((f - 32) * 5 / 9);
           }
      
           public double convertToFarenheit(double c) {
                return ((c * 9 / 5) + 32);
           }
      }
      

       

      The interface

       

      public interface TemperatureConverter {
              public double convertToCelsius(double f);
              public double convertToFarenheit(double c);
      }
      

       

       

      I try

      Removing the interface and just injecting the EJB the first test fail like i should.

       

      The test

      import javax.ejb.EJB;
      import org.jboss.arquillian.api.Deployment;
      import org.jboss.arquillian.testng.Arquillian;
      import org.jboss.shrinkwrap.api.ArchivePaths;
      import org.jboss.shrinkwrap.api.ShrinkWrap;
      import org.jboss.shrinkwrap.api.spec.JavaArchive;
      import org.jboss.shrinkwrap.impl.base.asset.ByteArrayAsset;
      import org.testng.Assert;
      import org.testng.annotations.Test;
      public class TemperatureConverterTest extends Arquillian {
       
         @EJB
         private TemperatureConverterImpl converter;
         @Test
         public void testConvertToCelsius() {
           Assert.fail();
           Assert.assertEquals(converter.convertToCelsius(32d), 0d);
           Assert.assertEquals(converter.convertToCelsius(212d), 100d);
         }
       
       
         @Test
         public void testConvertToFarenheit() {
            Assert.assertEquals(converter.convertToFarenheit(0d), 32d);
            Assert.assertEquals(converter.convertToFarenheit(100d), 212d);
         }
         @Deployment
         public static JavaArchive createTestArchive() {
         JavaArchive ja = ShrinkWrap.create("test6.jar", JavaArchive.class)
         .addClass(TemperatureConverter.class)
         .addClass(TemperatureConverterImpl.class)
         .addManifestResource(new ByteArrayAsset(new byte[0]), ArchivePaths.create("beans.xml"));
         System.out.println();
         System.out.println("Printing classes");
         System.out.println();
         ja.toString(Formatters.SIMPLE);
         System.out.println();
      return ja;
        }
      }
      

       

      The EJB

       

      import javax.ejb.Local;
      import javax.ejb.Stateless;
      
      @Stateless
      public class TemperatureConverterImpl {
      
           public double convertToCelsius(double f) {
                return ((f - 32) * 5 / 9);
           }
      
           public double convertToFarenheit(double c) {
                return ((c * 9 / 5) + 32);
           }
      }
       
      

       

      Do any of you have any idea? I appreciate all help nad again great projekt.

       

      /Stefan