2

I'm getting the following exception on running a test case, for my service class.

javax.ejb.EJBException: No EJBContainer provider available: no provider names had been found.
    at javax.ejb.embeddable.EJBContainer.reportError(EJBContainer.java:216)
    at javax.ejb.embeddable.EJBContainer.createEJBContainer(EJBContainer.java:146)
    at javax.ejb.embeddable.EJBContainer.createEJBContainer(EJBContainer.java:102)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Code:

public class NativeServiceTest {

       static NativeService NativeService;
       private static Context  ctx;
       private static EJBContainer ejbContainer;

       @BeforeClass
       public static void setUpBeforeClass() throws Exception {

          ejbContainer = EJBContainer.createEJBContainer();      
          ctx = ejbContainer.getContext();
       }


       @AfterClass
       public static void tearDownAfterClass() throws Exception {
          ejbContainer.close();
        }


       @Test
       public void testGetCustomService() throws NamingException {
          Gson gson= new Gson();
          String id = "";
          NativeService converter = (NativeService) ctx.lookup("java:global/classes/NativeService");
          assertNotNull(converter);
          gson.fromJson(NativeService.getCustomService(id), Portlet.class);

       }
    }

NativeService.java

public class NativeService {

       @EJB
       DataService dataService;
       //
       //   
    }

I'm using jta-data-source for transaction.

Nidheesh
  • 4,390
  • 29
  • 87
  • 150
  • Does your `NativeService` have with any Session Bean annotation? – António Ribeiro Apr 19 '16 at 07:44
  • 1
    Ok, so first you need to have a EJBContainerProvider implementation in your classpath. You could use [Glassfish embedded](http://mvnrepository.com/artifact/org.glassfish.main.extras/glassfish-embedded-all). Then, you need to annotate your `NativeService` with a session bean annotation in order to be able to look it up on the JNDI context. – António Ribeiro Apr 19 '16 at 07:58
  • Thanks. I will try that. – Nidheesh Apr 20 '16 at 05:53
  • This post may also be applicable: https://stackoverflow.com/questions/20616873/using-embedded-glassfish-with-maven as I was having a similar issue in NetBeans but since I was using maven, it didn't auto setup as the tutorial implied. – MVinca May 08 '21 at 15:20

1 Answers1

0

NativeService needs to be an EJBBean, for example @Stateless. Also make sure the test is in the same project as the EJBBean.

kerner1000
  • 3,382
  • 1
  • 37
  • 57