1 Reply Latest reply on Sep 10, 2011 8:59 AM by samwun9988

    How to test web service with EJB3 annotation

    samwun9988

      Hi,

       

      I followed an EJB3 junit test example, it is based on jboss-ejb-api_3.1_spec.

      The sample project is based on maven2 and use OpenEJB for junit test.

       

      When I execute the command "mvn clean install", it thrown the following error:

       

       

      INFO - Deployed Application(path=classpath.ear)

      WARN - Injection data not found in JNDI context: jndiName='org.superbiz.testinjection.MoviesTest/service', target=org.superbiz.testinjection.MoviesTest/service

      WARN - Injection data not found in JNDI context: jndiName='org.superbiz.testinjection.MoviesTest/service', target=org.superbiz.testinjection.MoviesTest/service

      findAll

       

       

      I don't understand why the service object is not populated to the above directory.

      eg. the @EJB injection is failed when it is instantiating the CategoryManagerImple service object.

       

       

      I am using the following setup in my junit test file:

       

       

      @LocalClient

      public class MoviesTest extends TestCase {

       

       

          @EJB

          private Movies movies;

         

         @EJB

          private CategoryManagerImpl service;

       

       

          @Resource

          private UserTransaction userTransaction;

       

       

          @PersistenceContext

          private EntityManager entityManager;

       

       

          public void setUp() throws Exception {

              Properties p = new Properties();

              p.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory");

              p.put("movieDatabase", "new://Resource?type=DataSource");

              p.put("movieDatabase.JdbcDriver", "org.hsqldb.jdbcDriver");

              p.put("movieDatabase.JdbcUrl", "jdbc:hsqldb:mem:moviedb");

       

       

              InitialContext initialContext = new InitialContext(p);

       

       

              // Here's the fun part

              initialContext.bind("inject", this);

          }

       

       

      There may be some issue in my CategoryManagerImpl service class, but I couldn't figured out why this class can't be instantiated by @EJB in junit test:

       

      Here is my CategoryManagerImpl.java file:

       

       

      @Stateless

      //@LocalBean

      public class CategoryManagerImpl implements CategoryManager {

      //    @WebServiceRef(wsdlLocation = "META-INF/wsdl/www.ixsystems.com.au/category-ws-0.0.1-SNAPSHOT/services.wsdl")

          @WebServiceRef(wsdlLocation = "http://www.ixsystems.com.au/category-ws-0.0.1-SNAPSHOT/services?wsdl")

          private CategoryWebService_Service service;

         

          private static final Logger logger = Logger.getLogger(CategoryManagerImpl.class);

       

         

          @Override

                  public int add(int a, int b) {

                      return a+b;

          }

      }

       

      I also attached my sample project here for reference.

       

      Any suggestion would be very appreciated.

      Thanks

      Sam

        • 1. Re: How to test web service with EJB3 annotation
          samwun9988

          Hi, after refactored the web service classes and the junit test file, I got the following error from running maven test:

           

          INFO - Creating ServerService(id=httpejbd)

          INFO - Creating ServerService(id=cxf)

          INFO - Creating ServerService(id=admin)

          INFO - Creating ServerService(id=ejbd)

          INFO - Creating ServerService(id=ejbds)

          INFO - Initializing network services

            ** Starting Services **

            NAME                 IP              PORT 

            httpejbd             127.0.0.1       4204 

            admin thread         127.0.0.1       4200 

            ejbd                 127.0.0.1       4201 

            ejbd                 127.0.0.1       4203 

          -------

          Ready!

          WARN - Injection data not found in JNDI context: jndiName='org.superbiz.testinjection.MoviesTest/service', target=org.superbiz.testinjection.MoviesTest/service

          WARN - Injection data not found in JNDI context: jndiName='org.superbiz.testinjection.MoviesTest/service', target=org.superbiz.testinjection.MoviesTest/service

          findAll

          Tests run: 2, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 7.94 sec <<< FAILURE!

           

           

          Results :

           

           

          Tests in error:

            testFindAll(org.superbiz.testinjection.MoviesTest): Could not find service named null in wsdl http://www.ixsystems.com.au/category-ws-0.0.1-SNAPSHOT/services?wsdl

           

           

          Tests run: 2, Failures: 0, Errors: 1, Skipped: 0

           

           

          Here is my junit test file:

           

          public void testFindAll() throws Exception {

                  System.out.println("findAll");

                  userTransaction.begin();

                   try {

                      Service cmService = Service.create(new URL("http://www.ixsystems.com.au/category-ws-0.0.1-SNAPSHOT/services?wsdl"), null);

                       assertNotNull(cmService);

           

           

                       CategoryManager cat = cmService.getPort(CategoryManager.class);

           

           

                      assertNotNull(cat);

                      Collection expResult = null;