没有提供EJBContainer提供程序:未找到提供程序名称

时间:2016-04-19 04:39:40

标签: junit ejb

我在运行测试用例时遇到以下异常,对于我的服务类。

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)

代码:

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;
       //
       //   
    }

我使用jta-data-source进行交易。

1 个答案:

答案 0 :(得分:0)

NativeService必须是EJBBean,例如@Stateless。还要确保测试与EJBBean在同一项目中。